如何在Java中使对象符合垃圾收集的条件?

一个对象有资格 垃圾收集 如果其引用变量在执行过程中从程序中丢失。有时它们也被称为 遥不可及的物体 .

null

什么是对象的引用?

这个 运算符为对象动态分配内存并返回对该对象的引用。此引用是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
    

相关文章: 孤岛

本文由 阿波娃·辛格和高拉夫·米格拉尼 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享