使用对象后,将使用垃圾收集器类从内存中释放该对象。基于不存在对该对象的引用这一事实,销毁了这些对象。垃圾收集器类在需要销毁的对象上调用“ finalize”函数。
什么是孤岛?
当两个对象“ a”和“ b”彼此引用,而其他任何对象都未引用它们时,则称为隔离岛。
它是一组互相引用的对象,但它们没有被引用,而只是其他应用程序的其他对象。
注–单个未引用对象也是孤立的孤岛。
让我们看一个例子-
示例public class Demo{
Demo i;
public static void main(String[] args){
Demo my_ob_1 = new Demo();
System.out.println("Demo object one has been created");
Demo my_ob_2 = new Demo();
System.out.println("Demo object two has been created");
my_ob_1.i = my_ob_2;
my_ob_2.i = my_ob_1;
my_ob_1 = null;
my_ob_2 = null;
System.gc();
}
@Override
protected void finalize() throws Throwable{
System.out.println("The finalize method has been called on the object");
}
}
输出结果Demo object one has been created
Demo object two has been created
The finalize method has been called on the object
The finalize method has been called on the object
名为Demo的类包含主要功能。在此,将创建Demo类类型的变量。创建Demo类的实例,然后将第二个对象分配给第一个对象的Demo对象。第二个dobject也以相反的方式进行相同的操作。现在,将这两个对象都分配为null并调用'System.gc'函数。现在,“ finalize”功能已被覆盖。