System.gc(); can't collect at once until the object have not reference
you could try following: public class C4_13 { public static void test() { new Tank(true); } public static void main(String[] args) { test(); Tank t1 = new Tank(false); t1.empty();
System.gc(); } }
class Tank { boolean isFull;
public Tank(boolean isFull) { this.isFull = isFull; } public void fill() { System.out.println("Oil box is full !!"); isFull = true; } public void empty() { System.out.println("Oil box is empty"); isFull = false; }
public void finalize() { if(isFull) System.out.println("An Error occurred, Oil box must be empty !!!"); } } public class testeee { public static void test() { Tank t1 = new Tank(true); } public static void main(String[] args) { test(); Tank t1 = new Tank(false); t1.fill();
t1.empty(); Tank t2 = new Tank(true); t2.fill(); System.gc(); } }
can't collect at once until the object have not reference
you could try following:
public class C4_13 {
public static void test()
{
new Tank(true);
}
public static void main(String[] args) {
test();
Tank t1 = new Tank(false);
t1.empty();
System.gc();
}
}
boolean isFull;
public Tank(boolean isFull) {
this.isFull = isFull;
}
public void fill() {
System.out.println("Oil box is full !!");
isFull = true;
}
public void empty() {
System.out.println("Oil box is empty");
isFull = false;
}
public void finalize() {
if(isFull)
System.out.println("An Error occurred, Oil box must be empty !!!");
}
}
public class testeee {
public static void test()
{
Tank t1 = new Tank(true);
}
public static void main(String[] args) {
test();
Tank t1 = new Tank(false);
t1.fill();
t1.empty();
Tank t2 = new Tank(true);
t2.fill();
System.gc();
}
}
但是我想,如果把t2设成静态变量呢?
结果好像一样.
public class C4_13 {
public static void main(String[] args) {
{ <---------------这里加个block就会发现t2,t1被gc了
Tank t1 = new Tank(false);
t1.fill();
Tank t2 = new Tank(false); <----- 这里
t2.fill();
t1.empty();
}
System.gc();
}
}
现在我明白了