为什么不调用finalize?
我有几个关于 java 垃圾收集器的问题.
I have couple of questions regarding garbage collector in java.
Q1. 据我了解,当对象超出范围且 JVM 即将收集垃圾时,将调用 finalize().我认为 finalize() 方法是由垃圾收集器自动调用的,但在这种情况下它似乎不起作用.解释是什么?为什么需要我显式调用 finalize() 方法?
Q1.As far as I understand, finalize() gets called when object is out of scope and JVM is about to collect garbage. I thought finalize() method is called automatically by garbage collector, but it does not seems to work in this case. What is the explanation? Why is the need for me to explicitly call finalize() method?
public class MultipleConstruct {
int x,y;
public MultipleConstruct(int x)
{
this.x= x;
y=5;
System.out.println("ONE");
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
System.out.println("FINALIZED");
}
public static void main(String[] args) throws Throwable {
MultipleConstruct construct = new MultipleConstruct(3);
}
}
第二季度.另外,什么时候调用垃圾收集器?我知道 gc 是一个守护线程,由 JVM 根据剩余的堆大小调用.这是否意味着,JVM 等待程序使用资源的阈值限制,然后通知 gc 清除垃圾对象.
Q2. Also, when is garbage collector invoked? I understand gc is a daemon thread and invoked by JVM depending on heap size remaining. Does that mean, JVM waits for the program to use threshold limit of resources and then notify the gc to sweep garbage objects.
gc 如何解析循环引用?
How does gc resolved circular references?
推荐答案
finalize() 方法有很多要写的,坦白说要写很多,但总之:
There is a lot to finalize() method which is frankly a lot to write, but in short:
如果一个对象在它的 finalize 方法(如果有的话)运行后仍然无法访问,那么它就处于 finalized 状态.已完成的对象正在等待解除分配.请注意,VM 实现控制终结器何时运行.您最好自己进行清理而不是依赖终结器.使用终结器还可能留下在不确定的时间内无法恢复的关键资源.
An object is in the finalized state if it is still unreachable after its finalize method, if any, has been run. A finalized object is awaiting deallocation. Note that the VM implementation controls when the finalizer is run. You are almost always better off doing your own cleanup instead of relying on a finalizer. Using a finalizer can also leave behind critical resources that won't be recovered for an indeterminate amount of time.
在您的情况下,它不打印的原因是您不知道终结器线程何时调用 finalize() 方法.正在发生的事情是程序在打印任何内容之前终止.要检查它:编辑主代码中的代码(注意:这不能保证也不应该依赖它,但它仍然会打印一段时间)
In your case the reason it does not print is that you do not know when the finalizer thread will call the finalize() method. What is happening is that the program is terminating before anything can get printed. To check it: edit the code inside main code by( NOTE: this does not guarrantee nor should you should ever rely on it but still it does prints some time)
for(int i =0;i<1000000;i++)
{
MultipleConstruct construct = new MultipleConstruct(3);
construct = null;
}
使用 finalize() 有很多缺点,从在对象构造中花费更多时间到内存泄漏和内存不足的可能性.如果您在 finalize() 中强烈引用同一个对象,那么它永远不会被第二次调用,因此可能会使系统处于不希望的状态等等等等......您应该使用 finalize() 的唯一地方是作为安全网来处理任何资源,例如 InputStream 使用它来关闭(同样没有保证它会在您的程序仍然存在时运行).另一个使用它的地方是在垃圾收集器无法控制的地方使用本机.
There are a lot of disadvantages of using a finalize() right from taking more time in object construction to possibility of memory leakage and memory starvation. If you strongly refer to the same object inside the finalize() then it is never called the second time and thus can leave system in undesired state etc etc etc... The only place where you should use finalize() is as a safety net to dispose any resources like InputStream uses it to close (which again there is no guarrantee that it will will br run when your program is still alive). Another place to use it is while using natives where garbage collector has no control.
欲了解更多信息,请访问:
For more info visit:
http://jatinpuri.com/?p=106
相关文章