• Tags
  •         
  • www.breakyizhan.com
  •    

    如果对象在执行期间从程序中丢失了引用变量,则有资格进行垃圾回收。有时它们也称为无法访问的对象

    什么是对象的引用?

    所述运营商动态地分配存储器的对象,并返回对它的引用。此引用是new分配的对象在内存中的地址。引用是指示存储对象的变量,方法等的位置的地址。

    分配给变量或作为参数传递给方法时,实际上不使用这些对象。对象的引用随处可见。例:

    Box mybox = new Box(); //引用对象
    

    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

     
    转载请保留页面地址:https://www.breakyizhan.com/java/4985.html