Home  >  Q&A  >  body text

java类中的成员成员变量赋值之后什么时候会被回收,有什么办法监听到某一个对象被回收吗

java类中的成员成员变量赋值之后什么时候会被回收,有什么办法监听到某一个对象被回收吗

天蓬老师天蓬老师2744 days ago670

reply all(2)I'll reply

  • 阿神

    阿神2017-04-18 10:52:02

    Whether an object is recycled is not judged simply by whether the current object is referenced.

    JVM uses the reachability analysis algorithm to determine whether to recycle an object. Nodes that are unreachable by GC ROOT will be marked. If an object is marked twice, it will be recycled. As for this reachability analysis algorithm, you can search it on Baidu yourself. The principle is very simple.

    So what method is used to monitor whether an object is recycled? Of course I used fianlize;

    Please see the code:

    public class Test {
        private static Test TEST= null;
        public static void main(String args[]) {
            TEST = new Test();
            TEST = null;
            System.gc();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(TEST);
        }
        @Override
        public void finalize() throws Throwable {
            System.out.println("要死了要死了要死了!");
        } 
    }
    

    Execution result:

    要死了要死了要死了!
    null

    Does that mean that after executing finalize, the object will definitely be recycled? In fact, it is not necessarily true that the object only has one chance to save itself when finalize is called, as follows:

    
    public class Test {
        private static Test TEST= null;
        public static void main(String args[]) {
            TEST = new Test();
            
            TEST = null;
            System.gc();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(TEST);
            
            TEST = null;
            System.gc();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(TEST);
        }
        
        @Override
        public void finalize() throws Throwable {
            System.out.println("要死了要死了要死了!");
            TEST = this;
        } 
    }

    The execution results are as follows:

    要死了要死了要死了!
    com.Test@1aa9f99
    null

    You can see that during the first garbage collection, the finalize method assigns a new reference to the current recycling object to avoid being recycled. However, the finalize method can only be called once for an object, and it will not be called during the second recycling. was called.

    We can conclude from the above two examples: finalize can monitor an object to be recycled, but there is no guarantee that the object on which finalize is called will be recycled. At the same time, an object will not trigger finalize when it is marked for recycling for the second time. ! If you want to absolutely monitor whether an object is recycled, you can only add the parameter -XX:+PrintGCDetails to the JVM to analyze the GC log

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:52:02

    Not necessarily when. Isn't it possible to use the destructor of that object class?

    reply
    0
  • Cancelreply