为什么最终确定不被调用? [英] Why is finalize not being called?

查看:75
本文介绍了为什么最终确定不被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Q1.据我所知,当对象超出范围并且JVM被调用时,finalize()会被调用。即将收集垃圾。我认为finalize()方法被垃圾回收器自动调用,但在这种情况下似乎不起作用。什么是解释?为什么需要我明确调用finalize()方法?

  public class MultipleConstruct {
int x,y ;
public MultipleConstruct(int x)
{
this.x = x;
y = 5;
System.out.println(ONE);

$ b @Override
protected void finalize()throws Throwable {
// TODO自动生成的方法存根
super.finalize();
System.out.println(FINALIZED);

public static void main(String [] args)throws Throwable {
MultipleConstruct construct = new MultipleConstruct(3);
}
}



Q2。另外,什么时候垃圾收集器被调用?我知道gc是一个守护进程线程,由JVM根据剩余的堆大小调用。这是否意味着,JVM等待程序使用资源的阈值限制,然后通知gc清除垃圾对象。



编辑:gc如何解析循环引用?



一个对象处于finalized状态if它的finalize方法(如果有的话)已经运行后仍然无法访问。最终的对象正在等待释放。请注意,VM实施控制终结器运行的时间。你几乎总是做自己的清理而不是依靠终结者。使用终结器也可能会留下不能在无限期的时间内恢复的关键资源。

在你的情况中,它不打印的原因是你不知道finalizer线程何时调用finalize()方法。发生的事情是,在打印任何内容之前程序正在终止。要检查它:
编辑主代码中的代码(注意:这不保证也不应该依赖它,但它仍然会打印一些时间)

  for(int i = 0; i <1000000; i ++)
{
MultipleConstruct construct = new MultipleConstruct(3);
construct = null;

$ / code>

使用finalize()方法有很多缺点,在对象构造中发生内存泄漏和内存不足的可能性。如果你强烈地在finalize()中引用同一个对象,那么它永远不会被第二次调用,因此可能会让系统处于不希望的状态等等等等等等等等。
您应该使用finalize()的唯一地方是作为一个安全网来处理任何资源,比如InputStream使用它来关闭(当你的程序还活着的时候,它再次没有保证它会运行)。另一个使用它的地方是使用垃圾收集器无法控制的本机。



欲了解更多信息,请访问:


http://jatinpuri.com/?p=106 p>


I have couple of questions regarding garbage collector in java.

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);
    }
}

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.

EDIT: How does gc resolved circular references?

解决方案

There is a lot to finalize() method which is frankly a lot to write, but in short:

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.

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;
    }

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

这篇关于为什么最终确定不被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆