gc():
Calling the gc method suggests that the Java Virtual
Machine expend effort toward recycling unused objects in order to
make the memory they currently occupy available for quick reuse.
When control returns from the method call, the Java Virtual
Machine has made a best effort to reclaim space from all discarded
objects.runFinalization()
Runs the finalization methods of any objects pending finalization.
Calling this method suggests that the Java Virtual Machine expend
effort toward running the <code>finalize</code> methods of objects
that have been found to be discarded but whose <code>finalize</code>
methods have not yet been run. When control returns from the
method call, the Java Virtual Machine has made a best effort to
complete all outstanding finalizations. 从上面看出,他们释放的方法不同。另外,java的垃圾可以不用管,它自身有垃圾处理机制,当然,如果你想要获得更高的效率可以自己做。在这一点上比VC要方便,要求的那么严格。
欢迎讨论。
Calling the gc method suggests that the Java Virtual
Machine expend effort toward recycling unused objects in order to
make the memory they currently occupy available for quick reuse.
When control returns from the method call, the Java Virtual
Machine has made a best effort to reclaim space from all discarded
objects.runFinalization()
Runs the finalization methods of any objects pending finalization.
Calling this method suggests that the Java Virtual Machine expend
effort toward running the <code>finalize</code> methods of objects
that have been found to be discarded but whose <code>finalize</code>
methods have not yet been run. When control returns from the
method call, the Java Virtual Machine has made a best effort to
complete all outstanding finalizations. 从上面看出,他们释放的方法不同。另外,java的垃圾可以不用管,它自身有垃圾处理机制,当然,如果你想要获得更高的效率可以自己做。在这一点上比VC要方便,要求的那么严格。
欢迎讨论。
runFinalizersOnExit()看字面意思就知道了。
2.runFinalization()是不是运行所有没有运行过finalize()的对象(不管垃圾与否,正如上面wks9527所说)?那回收他们的空间么?
3.gc()+runFinalization()这样有什么作用?不是多此一举么?(但是thinking in java书上开始前面的例子就是先运行gc()再运行runFinalization().
4.runFinalizeOnExit()我看字面意思好像还是不是很明白。是说程序退出时运行runFinalization()么? 那是不是说一般程序退出时是不会调用runFinalization(),也就是说即使程序结束也有可能没有释放完所有申请的空间?
我对C++比较熟悉(正在学java),所以对于构造函数,析构函数很熟悉的。解释性质的就不给分了,如果能对比,谈区别,准确的,给高分。
能正确回答上面所有问题的,给高分,其他酌情给分
2。runFinalization()是通知JVM调用每一个object类的finalize(),如果我们想在一个类被收掉以前做一些除了释放空间以外其它的事,overload这个类的finalize()很必要(我可没试过).
3。runFinalizatonOnExit()很明显,相当于runFinalization();exit();这两句话加在了一起。
这些都是从<<thinking in java>>里我自己理解出来的,对不对还请大家看一下,虽然java不用自己去delete一个对像的空间,但这毕竟是它提供的一个机制。还有一点儿,就是为什么要先gc()后runFinalization()的原因,我是这样理解的,gc()首先通知JVM去收集垃圾,但只是通知,还没有马上就做,只是将要收集的垃圾统计一下(呵呵,自己的名词哦),然后用runFinalization()就是通知收集的时候必要的一定要调用那些个finalize(),这样你自己在finalize()里定义的一引起功能就能在这个对像被收之前被执行,不至于误事。
还有一点就是,我查阅过资料,说的runFinalization()并不是运行所有对象的finalize(),而是运行那些还没有运行过finalize()的垃圾对象。注意说的是垃圾对象,且还没有运行过finalize()。也就是说其实gc()的工作里面已经包含了runFinalization(),相当于gc()先回收可以回收的空间,然后再运行runFinalization()。
希望大家讨论一下。谢谢