public class A {
private int a;
public A(int a) {this.a= a;}
protected void finalize() {System.out.println("开始清理对象"+a);}
}
(1)public class Test1 {
public static void main(String[] args) {
{
A a = new A(1);
A aa = new A(11);
}
System.gc();
}
}
无输出结果
================================================
(2)public class Test2 {
public static void main(String[] args) {
{
A a = new A(1);
A aa = new A(11);
}
{
A a = new A(2);
}
System.gc();
}
}
输出结果:
开始清理对象1
===================================================
(3)public class Test3 {
public static void main(String[] args) {
{
A a =new A(1);
A aa =new A(11);
}
{
A a =new A(2);
A aa =new A(22);
}
System.gc();
}
}
输出结果:
开始清理对象11
开始清理对象1
==================================================
(4)public class Test4 {
public static void main(String[] args) {
{
A a =new A(1);
A aa =new A(11);
}
{
A a =new A(2);
A aa =new A(22);
}
A a =new A(3);
System.gc();
}
}
输出结果:
开始清理对象2
开始清理对象11
开始清理对象1
=================================================
(5)public class Test5 {
public static void main(String[] args) {
{
A a =new A(1);
A aa =new A(11);
}
{
A a =new A(2);
A aa =new A(22);
}
A a =new A(3);
A aa =new A(33);
System.gc();
}
}
输出结果:
开始清理对象22
开始清理对象2
开始清理对象11
开始清理对象1
网友评论