📜  如何使对象有资格在Java中进行垃圾回收?

📅  最后修改于: 2022-05-13 01:54:55.083000             🧑  作者: Mango

如何使对象有资格在Java中进行垃圾回收?

如果一个对象的引用变量在执行过程中从程序中丢失,那么它就有资格被垃圾回收。有时它们也被称为不可访问对象

什么是对象的引用?

new运算符为对象动态分配内存并返回对它的引用。这个引用是new分配的对象在内存中的地址。引用是指示对象变量、方法等存储位置的地址。

当分配给变量或作为参数传递给方法时,这些对象实际上并没有被使用。对对象的引用无处不在。例子:

Box mybox =  new Box();   //referencing to object

Java中不可达对象的作用

在Java中,运行时分配的内存,即堆区域可以通过垃圾收集过程释放。它只是一种释放程序员未使用的内存的方法。只有不再引用它们的对象才有资格在Java中进行垃圾回收。

使对象符合垃圾回收条件的方法:

请注意,在丢弃所有对它的引用之前,该对象不能成为垃圾回收的候选对象。

  1. 在方法内创建的对象:当调用方法时,它会进入堆栈框架。当方法从堆栈中弹出时,它的所有成员都死了,如果在其中创建了一些对象,那么这些对象在方法执行后变得不可访问或匿名,因此有资格进行垃圾回收
    。例子:
    /* Java program to demonstrate that 
    objects created inside a method will becomes
    eligible for gc after method execution terminate */
      
    class Test
    {
          
        // to store object name
        String obj_name;
          
        public Test(String obj_name) 
        {
            this.obj_name = obj_name;
        }
          
        static void show()
        {
            //object t1 inside method becomes unreachable when show() removed
            Test t1 = new Test("t1"); 
            display();
              
        }
        static void display()
        {
            //object t2 inside method becomes unreachable when display() removed
            Test t2 = new Test("t2"); 
        }
          
        // Driver method
        public static void main(String args[])
        {
            // calling show()
            show();
              
            // calling garbage collector
            System.gc();
        }
          
        @Override
        /* Overriding finalize method to check which object
        is garbage collected */
        protected void finalize() throws Throwable 
        {
            // will print name of object
            System.out.println(this.obj_name + " successfully garbage collected");
        }
    }
    

    输出:

    t2 successfully garbage collected
    t1 successfully garbage collected
    

    注意:如果方法返回在其中创建的对象,并且我们使用引用类型变量存储此对象引用,则它不再符合垃圾回收条件。

  2. 重新分配引用变量:当一个对象的引用 id 被引用到其他对象的引用 id 时,前一个对象不再引用它并且变得无法访问,因此有资格进行垃圾回收。示例:
    /* Java program to demonstrate gc
     when one object referred to other object */
      
    class Test
    {
        // to store object name
        String obj_name;
          
        public Test(String obj_name) 
        {
            this.obj_name = obj_name;
        }
          
        // Driver method
        public static void main(String args[])
        {
            Test t1 = new Test("t1");
            Test t2 = new Test("t2");
              
            //t1 now referred to t2
            t1 = t2;
              
            // calling garbage collector
            System.gc();
        }
          
        @Override
        /* Overriding finalize method to check which object
         is garbage collected */
        protected void finalize() throws Throwable 
        {
            // will print name of object
            System.out.println(this.obj_name + " successfully garbage collected");
        }
    }
    

    输出:

    t1 successfully garbage collected
    
  3. 使引用变量无效:当一个对象的所有引用变量都更改为 NULL 时,它变得无法访问,从而有资格进行垃圾回收。示例:
    /* Java program to demonstrate gc
     when object reference changed to NULL */
      
    class Test
    {
        // to store object name
        String obj_name;
          
        public Test(String obj_name) 
        {
            this.obj_name = obj_name;
        }
          
        // Driver method
        public static void main(String args[])
        {
            Test t1 = new Test("t1");
               
            /* t1 being used for some purpose in program */
       
            /* When there is no more use of t1, make the object
               referred by t1 eligible for garbage collection */        
            t1 = null;
       
            // calling garbage collector
            System.gc();
        }
          
        @Override
        /* Overriding finalize method to check which object
         is garbage collected */
        protected void finalize() throws Throwable 
        {
            // will print name of object
            System.out.println(this.obj_name + " successfully garbage collected");
        }
    }
    

    输出:

    t1 successfully garbage collected
    
  4. 匿名对象:匿名对象的引用 id 不存储在任何地方。因此,它变得无法访问。
    例子:
    /* Java program to demonstrate gc
     of anonymous objects */
      
    class Test
    {
        // to store object name
        String obj_name;
          
        public Test(String obj_name) 
        {
            this.obj_name = obj_name;
        }
          
        // Driver method
        public static void main(String args[])
        {
            //anonymous object without reference id
            new Test("t1"); 
       
            // calling garbage collector
            System.gc();
        }
          
        @Override
        /* Overriding finalize method to check which object
         is garbage collected */
        protected void finalize() throws Throwable 
        {
            // will print name of object
            System.out.println(this.obj_name + " successfully garbage collected");
        }
    }
    

    输出:

    t1 successfully garbage collected
    

相关文章:孤立岛