如何使对象有资格使用Java进行垃圾回收()

一个对象有资格成为垃圾收集如果其引用变量在执行期间从程序中丢失, 有时也称为无法到达的对象.
什么是物体的参照?
【如何使对象有资格使用Java进行垃圾回收()】new操作符为对象动态分配内存并返回对该对象的引用。该引用是new分配的对象在内存中的地址。引用是一个地址, 指示对象的变量, 方法等的存储位置。
将对象分配给变量或作为参数传递给方法时, 实际上并没有使用这些对象。对对象的引用随处可见。例:

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

Java中不可访问对象的作用
在Java中, 运行时分配的内存(即堆区域)可以通过垃圾回收过程释放。它不过是释放内存的一种方法, 程序员没有使用它。只有不再引用它们的对象才有资格在Java中进行垃圾回收。
使对象有资格进行垃圾收集的方法:
请注意, 在放弃对该对象的所有引用之前, 该对象不能成为垃圾回收的候选对象。
在方法内部创建的对象:
调用方法时, 它将进入堆栈框架。当方法从堆栈中弹出时, 其所有成员都会死亡, 如果在其中创建了一些对象, 则在方法执行后这些对象将变得不可访问或匿名, 从而有资格进行垃圾回收
例子:
/* 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

注意 :如果某个方法返回了在其内部创建的对象, 并且我们使用引用类型变量存储了该对象引用, 则该对象不再符合垃圾回收的条件。
重新分配参考变量:
当一个对象的引用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

取消引用变量:
当一个对象的所有引用变量都更改为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

匿名对象:匿名对象的参考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

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

    推荐阅读