Skip to content

Commit b4975cc

Browse files
author
chenweijie
committed
循环引用实例
1 parent 77db989 commit b4975cc

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
package com.chen.api.util.jvm.chapter3;
22

33
/**
4+
* 循环引用的垃圾回收
5+
*
46
* @author : chen weijie
57
* @Date: 2018-10-24 9:20 AM
68
*/
79
public class ForEachReferenceDemo {
10+
11+
12+
public Object instance = null;
13+
private static final int _1MB = 1024 * 1024;
14+
15+
//这个成员变量的唯一作用就是站点内存,一边在GC日志中可以看清楚是否被回收过
16+
17+
private byte[] bigSize = new byte[2 * _1MB];
18+
19+
public static void testGC() {
20+
21+
ForEachReferenceDemo objectA = new ForEachReferenceDemo();
22+
ForEachReferenceDemo objectB = new ForEachReferenceDemo();
23+
objectA.instance = objectA;
24+
objectB.instance = objectB;
25+
objectA = null;
26+
objectB = null;
27+
System.gc();
28+
29+
30+
}
31+
32+
public static void main(String[] args) {
33+
testGC();
34+
}
35+
36+
837
}

0 commit comments

Comments
 (0)