Code
1package test;
2
3class TestGC {
4 private String str = "hello";
5
6 TestGC(String str) {
7 this.str = str;
8 }
9
10 public void finalize() {
11 System.out.println(str);
12 }
13}
14
15public class Hello {
16
17 /** *//**
18 * @param args
19 */
20 public static void main(String[] args) {
21 // TODO 自动生成方法存根
22 System.out.println("hello");
23
24 TestGC test = new TestGC("test1");
25 test = new TestGC("test2");
26 test = null;//注释掉这一句,test1被回收。加上则先回收test2,后test1
27 System.gc();
28 }
29
30}
31
1package test;
2
3class TestGC {
4 private String str = "hello";
5
6 TestGC(String str) {
7 this.str = str;
8 }
9
10 public void finalize() {
11 System.out.println(str);
12 }
13}
14
15public class Hello {
16
17 /** *//**
18 * @param args
19 */
20 public static void main(String[] args) {
21 // TODO 自动生成方法存根
22 System.out.println("hello");
23
24 TestGC test = new TestGC("test1");
25 test = new TestGC("test2");
26 test = null;//注释掉这一句,test1被回收。加上则先回收test2,后test1
27 System.gc();
28 }
29
30}
31