<small id='Unfbz'></small><noframes id='Unfbz'>

    <tfoot id='Unfbz'></tfoot>

      • <bdo id='Unfbz'></bdo><ul id='Unfbz'></ul>

      <i id='Unfbz'><tr id='Unfbz'><dt id='Unfbz'><q id='Unfbz'><span id='Unfbz'><b id='Unfbz'><form id='Unfbz'><ins id='Unfbz'></ins><ul id='Unfbz'></ul><sub id='Unfbz'></sub></form><legend id='Unfbz'></legend><bdo id='Unfbz'><pre id='Unfbz'><center id='Unfbz'></center></pre></bdo></b><th id='Unfbz'></th></span></q></dt></tr></i><div id='Unfbz'><tfoot id='Unfbz'></tfoot><dl id='Unfbz'><fieldset id='Unfbz'></fieldset></dl></div>

      <legend id='Unfbz'><style id='Unfbz'><dir id='Unfbz'><q id='Unfbz'></q></dir></style></legend>

      为什么不调用finalize?

      Why is finalize not being called?(为什么不调用finalize?)
    1. <legend id='AuAOT'><style id='AuAOT'><dir id='AuAOT'><q id='AuAOT'></q></dir></style></legend>

      • <small id='AuAOT'></small><noframes id='AuAOT'>

        <i id='AuAOT'><tr id='AuAOT'><dt id='AuAOT'><q id='AuAOT'><span id='AuAOT'><b id='AuAOT'><form id='AuAOT'><ins id='AuAOT'></ins><ul id='AuAOT'></ul><sub id='AuAOT'></sub></form><legend id='AuAOT'></legend><bdo id='AuAOT'><pre id='AuAOT'><center id='AuAOT'></center></pre></bdo></b><th id='AuAOT'></th></span></q></dt></tr></i><div id='AuAOT'><tfoot id='AuAOT'></tfoot><dl id='AuAOT'><fieldset id='AuAOT'></fieldset></dl></div>
          <bdo id='AuAOT'></bdo><ul id='AuAOT'></ul>
        • <tfoot id='AuAOT'></tfoot>
                  <tbody id='AuAOT'></tbody>
                本文介绍了为什么不调用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

                这篇关于为什么不调用finalize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Maven JAVA_HOME environment variable is not defined correctly, but it is(Maven JAVA_HOME 环境变量未正确定义,但它是)
                Java System Environment Variable(Java 系统环境变量)
                Java -classpath option(Java -classpath 选项)
                Read environment variable in SpringBoot(在 SpringBoot 中读取环境变量)
                Tomcat 8 - context.xml use Environment Variable in Datasource(Tomcat 8 - context.xml 在数据源中使用环境变量)
                Issue with JAVA_HOME(JAVA_HOME 的问题)

                  <tbody id='Nv9YY'></tbody>

                  <tfoot id='Nv9YY'></tfoot>

                      • <bdo id='Nv9YY'></bdo><ul id='Nv9YY'></ul>

                        <small id='Nv9YY'></small><noframes id='Nv9YY'>

                        • <i id='Nv9YY'><tr id='Nv9YY'><dt id='Nv9YY'><q id='Nv9YY'><span id='Nv9YY'><b id='Nv9YY'><form id='Nv9YY'><ins id='Nv9YY'></ins><ul id='Nv9YY'></ul><sub id='Nv9YY'></sub></form><legend id='Nv9YY'></legend><bdo id='Nv9YY'><pre id='Nv9YY'><center id='Nv9YY'></center></pre></bdo></b><th id='Nv9YY'></th></span></q></dt></tr></i><div id='Nv9YY'><tfoot id='Nv9YY'></tfoot><dl id='Nv9YY'><fieldset id='Nv9YY'></fieldset></dl></div>
                          <legend id='Nv9YY'><style id='Nv9YY'><dir id='Nv9YY'><q id='Nv9YY'></q></dir></style></legend>