测试类
@Slf4j
public class ThreadGcTest {
// 会回收
private final WeakReference<GcThread> gcThreadWeakReference = new WeakReference<>(new GcThread("弱引用"));
private final GcThread gcThread = new GcThread("强引用");
//不会回收,OOM回收
private final SoftReference<GcThread> gcThreadSoftReference = new SoftReference<>(new GcThread("软引用"));
@Test
public void testGc() throws InterruptedException {
GcThread weakGcThread = new GcThread("弱引用2");
WeakReference<GcThread> gcThreadWeakReference2 = new WeakReference<>(weakGcThread);
ThreadGcTest threadGcTest = new ThreadGcTest();
threadGcTest.gcThreadWeakReference.get().start();
threadGcTest.gcThreadSoftReference.get().start();
threadGcTest.gcThread.start();
gcThreadWeakReference2.get().start();
GcThread phantomGcThread = new GcThread("虚引用");
final ReferenceQueue<GcThread> referenceQueue = new ReferenceQueue<>();
PhantomReference<GcThread> gcThreadPhantomReference = new PhantomReference<>(phantomGcThread, referenceQueue);
phantomGcThread = null;
System.gc();
Thread.sleep(3000);
System.out.println(referenceQueue.poll());
}
public static final class GcThread extends Thread{
private String name;
public GcThread(String name) {
this.name = name;
}
@Override
public void run() {
log.info("GC Test:{}", name);
}
@Override
protected void finalize() throws Throwable {
log.info("finalize:{}", name);
}
}
}
运行VM参数
-verbose:gc -Xms4m -Xmx4m -Xmn2m -XX:+PrintGCDetails
运行结果
[GC (Allocation Failure) [PSYoungGen: 1024K->497K(1536K)] 1024K->753K(3584K), 0.0005181 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1502K->504K(1536K)] 1758K->1097K(3584K), 0.0007247 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1523K->512K(1536K)] 2117K->1451K(3584K), 0.0005652 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1536K->512K(1536K)] 2475K->1651K(3584K), 0.0009723 secs] [Times: user=0.23 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1536K->512K(1536K)] 2675K->1787K(3584K), 0.0007451 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1536K->512K(1536K)] 2811K->1997K(3584K), 0.0006232 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1536K->512K(1536K)] 3021K->2395K(3584K), 0.0009188 secs] [Times: user=0.01 sys=0.02, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 512K->0K(1536K)] [ParOldGen: 1883K->1466K(2048K)] 2395K->1466K(3584K), [Metaspace: 5754K->5754K(1056768K)], 0.0243290 secs] [Times: user=0.00 sys=0.00, real=0.02 secs]
[GC (Allocation Failure) [PSYoungGen: 1018K->402K(1536K)] 2484K->1876K(3584K), 0.0026897 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1426K->512K(1536K)] 2900K->2101K(3584K), 0.0010663 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 512K->0K(1536K)] [ParOldGen: 1589K->1734K(2048K)] 2101K->1734K(3584K), [Metaspace: 5903K->5903K(1056768K)], 0.0048280 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1734K->1677K(2048K)] 2758K->1677K(3584K), [Metaspace: 6193K->6193K(1056768K)], 0.0177952 secs] [Times: user=0.20 sys=0.00, real=0.02 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1677K->1696K(2048K)] 2701K->1696K(3584K), [Metaspace: 6490K->6490K(1056768K)], 0.0051967 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1696K->1704K(2048K)] 2720K->1704K(3584K), [Metaspace: 6550K->6550K(1056768K)], 0.0049767 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1704K->1711K(2048K)] 2728K->1711K(3584K), [Metaspace: 6591K->6591K(1056768K)], 0.0058300 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1711K->1761K(2048K)] 2735K->1761K(3584K), [Metaspace: 6666K->6666K(1056768K)], 0.0060585 secs] [Times: user=0.06 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1761K->1896K(2048K)] 2785K->1896K(3584K), [Metaspace: 6920K->6920K(1056768K)], 0.0084006 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1896K->1818K(2048K)] 2920K->1818K(3584K), [Metaspace: 6975K->6975K(1056768K)], 0.0055221 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1818K->1823K(2048K)] 2842K->1823K(3584K), [Metaspace: 7024K->7024K(1056768K)], 0.0067983 secs] [Times: user=0.23 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1022K->0K(1536K)] [ParOldGen: 1823K->1876K(2048K)] 2845K->1876K(3584K), [Metaspace: 7222K->7222K(1056768K)], 0.0066661 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1876K->1944K(2048K)] 2900K->1944K(3584K), [Metaspace: 7340K->7340K(1056768K)], 0.0071248 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1020K->0K(1536K)] [ParOldGen: 1944K->1959K(2048K)] 2964K->1959K(3584K), [Metaspace: 7421K->7421K(1056768K)], 0.0043371 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1959K->1946K(2048K)] 2983K->1946K(3584K), [Metaspace: 7497K->7497K(1056768K)], 0.0050759 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1946K->1921K(2048K)] 2970K->1921K(3584K), [Metaspace: 7544K->7544K(1056768K)], 0.0051657 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1921K->1949K(2048K)] 2945K->1949K(3584K), [Metaspace: 7682K->7682K(1056768K)], 0.0050662 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->0K(1536K)] [ParOldGen: 1949K->2005K(2048K)] 2973K->2005K(3584K), [Metaspace: 7792K->7792K(1056768K)], 0.0051494 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 1019K->0K(1536K)] [ParOldGen: 2005K->2003K(2048K)] 3025K->2003K(3584K), [Metaspace: 7868K->7868K(1056768K)], 0.0084227 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1021K->71K(1536K)] [ParOldGen: 2003K->2016K(2048K)] 3024K->2087K(3584K), [Metaspace: 8042K->8042K(1056768K)], 0.0251887 secs] [Times: user=0.00 sys=0.00, real=0.03 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->244K(1536K)] [ParOldGen: 1986K->1986K(2048K)] 3010K->2230K(3584K), [Metaspace: 8705K->8705K(1058816K)], 0.0056568 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->275K(1536K)] [ParOldGen: 1986K->1986K(2048K)] 3010K->2262K(3584K), [Metaspace: 8781K->8781K(1058816K)], 0.0060858 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->300K(1536K)] [ParOldGen: 1986K->1986K(2048K)] 3010K->2287K(3584K), [Metaspace: 8797K->8797K(1058816K)], 0.0086090 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->330K(1536K)] [ParOldGen: 1986K->1986K(2048K)] 3010K->2317K(3584K), [Metaspace: 8850K->8850K(1058816K)], 0.0089348 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->319K(1536K)] [ParOldGen: 1986K->1985K(2048K)] 3010K->2304K(3584K), [Metaspace: 8890K->8890K(1058816K)], 0.0088666 secs] [Times: user=0.17 sys=0.02, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->317K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2303K(3584K), [Metaspace: 8923K->8923K(1058816K)], 0.0066455 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->337K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2322K(3584K), [Metaspace: 9039K->9039K(1058816K)], 0.0062157 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->343K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2328K(3584K), [Metaspace: 9168K->9168K(1058816K)], 0.0154450 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->354K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2340K(3584K), [Metaspace: 9194K->9194K(1058816K)], 0.0063747 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->368K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2354K(3584K), [Metaspace: 9231K->9231K(1058816K)], 0.0068697 secs] [Times: user=0.16 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->365K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2351K(3584K), [Metaspace: 9353K->9353K(1058816K)], 0.0103938 secs] [Times: user=0.23 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->555K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2540K(3584K), [Metaspace: 9648K->9648K(1058816K)], 0.0103580 secs] [Times: user=0.20 sys=0.02, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->557K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2543K(3584K), [Metaspace: 9665K->9665K(1058816K)], 0.0074087 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->559K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2544K(3584K), [Metaspace: 9692K->9692K(1058816K)], 0.0277064 secs] [Times: user=0.00 sys=0.00, real=0.03 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->561K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2546K(3584K), [Metaspace: 9713K->9713K(1058816K)], 0.0070784 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->578K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2563K(3584K), [Metaspace: 9740K->9740K(1058816K)], 0.0071649 secs] [Times: user=0.17 sys=0.02, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1023K->602K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2587K(3584K), [Metaspace: 9784K->9784K(1058816K)], 0.0062555 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1022K->626K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3007K->2611K(3584K), [Metaspace: 9806K->9806K(1058816K)], 0.0065551 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
16:40:47.055 [Finalizer] INFO catl.web.test.ThreadGcTest - finalize:弱引用
[Full GC (Ergonomics) [PSYoungGen: 1024K->596K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2581K(3584K), [Metaspace: 9831K->9831K(1058816K)], 0.0066970 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->643K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 3009K->2628K(3584K), [Metaspace: 9869K->9869K(1058816K)], 0.0073870 secs] [Times: user=0.05 sys=0.00, real=0.01 secs]
[Full GC (System.gc()) [PSYoungGen: 771K->644K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 2756K->2629K(3584K), [Metaspace: 9880K->9880K(1058816K)], 0.0108262 secs] [Times: user=0.24 sys=0.00, real=0.01 secs]
16:40:47.091 [Thread-5] INFO catl.web.test.ThreadGcTest - GC Test:弱引用
16:40:47.091 [Thread-7] INFO catl.web.test.ThreadGcTest - GC Test:软引用
16:40:47.102 [Finalizer] INFO catl.web.test.ThreadGcTest - finalize:虚引用
16:40:47.102 [Thread-6] INFO catl.web.test.ThreadGcTest - GC Test:强引用
16:40:47.102 [Thread-4] INFO catl.web.test.ThreadGcTest - GC Test:弱引用2
[Full GC (Ergonomics) [PSYoungGen: 1006K->800K(1536K)] [ParOldGen: 1985K->1985K(2048K)] 2991K->2786K(3584K), [Metaspace: 9971K->9971K(1058816K)], 0.0258122 secs] [Times: user=0.23 sys=0.00, real=0.03 secs]
16:40:47.349 [Finalizer] INFO catl.web.test.ThreadGcTest - finalize:弱引用
[Full GC (Ergonomics) [PSYoungGen: 1015K->804K(1536K)] [ParOldGen: 1985K->1968K(2048K)] 3001K->2772K(3584K), [Metaspace: 9971K->9971K(1058816K)], 0.0144685 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
java.lang.ref.PhantomReference@3b2da18f
[Full GC (Ergonomics) [PSYoungGen: 1024K->773K(1536K)] [ParOldGen: 1968K->1939K(2048K)] 2992K->2713K(3584K), [Metaspace: 9981K->9980K(1058816K)], 0.0295317 secs] [Times: user=0.02 sys=0.00, real=0.03 secs]
16:40:50.141 [Finalizer] INFO catl.web.test.ThreadGcTest - finalize:软引用
16:40:50.141 [Finalizer] INFO catl.web.test.ThreadGcTest - finalize:强引用
16:40:50.141 [Finalizer] INFO catl.web.test.ThreadGcTest - finalize:弱引用2
[Full GC (Ergonomics) [PSYoungGen: 1024K->694K(1536K)] [ParOldGen: 1939K->1833K(2048K)] 2963K->2527K(3584K), [Metaspace: 10009K->9980K(1058816K)], 0.0275445 secs] [Times: user=0.01 sys=0.00, real=0.03 secs]
16:40:50.172 [Finalizer] INFO catl.web.test.ThreadGcTest - finalize:软引用
[Full GC (Ergonomics) [PSYoungGen: 1024K->717K(1536K)] [ParOldGen: 1833K->1833K(2048K)] 2857K->2550K(3584K), [Metaspace: 10004K->10004K(1058816K)], 0.0104237 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->152K(1536K)] [ParOldGen: 2016K->1988K(2048K)] 3040K->2140K(3584K), [Metaspace: 8173K->8173K(1056768K)], 0.0057501 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->151K(1536K)] [ParOldGen: 1988K->1988K(2048K)] 3012K->2139K(3584K), [Metaspace: 8310K->8310K(1056768K)], 0.0056015 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->167K(1536K)] [ParOldGen: 1988K->1988K(2048K)] 3012K->2155K(3584K), [Metaspace: 8359K->8359K(1056768K)], 0.0062082 secs] [Times: user=0.23 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->152K(1536K)] [ParOldGen: 1988K->1988K(2048K)] 3012K->2140K(3584K), [Metaspace: 8395K->8395K(1056768K)], 0.0086542 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1022K->201K(1536K)] [ParOldGen: 1988K->1988K(2048K)] 3010K->2189K(3584K), [Metaspace: 8465K->8465K(1056768K)], 0.0058031 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->207K(1536K)] [ParOldGen: 1988K->1986K(2048K)] 3012K->2194K(3584K), [Metaspace: 8568K->8568K(1056768K)], 0.0061667 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1024K->212K(1536K)] [ParOldGen: 1986K->1986K(2048K)] 3010K->2199K(3584K), [Metaspace: 8634K->8634K(1058816K)], 0.0066072 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1023K->731K(1536K)] [ParOldGen: 1833K->1833K(2048K)] 2856K->2564K(3584K), [Metaspace: 10029K->10029K(1058816K)], 0.0078427 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
16:40:50.196 [Finalizer] INFO catl.web.test.ThreadGcTest - finalize:强引用
Heap
PSYoungGen total 1536K, used 986K [0x00000000ffe00000, 0x0000000100000000, 0x0000000100000000)
eden space 1024K, 96% used [0x00000000ffe00000,0x00000000ffef6bb8,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
ParOldGen total 2048K, used 1833K [0x00000000ffc00000, 0x00000000ffe00000, 0x00000000ffe00000)
object space 2048K, 89% used [0x00000000ffc00000,0x00000000ffdca500,0x00000000ffe00000)
Metaspace used 10203K, capacity 11338K, committed 11648K, reserved 1058816K
class space used 1317K, capacity 1555K, committed 1664K, reserved 1048576K
Process finished with exit code 0
不带参数输出GC情况
16:43:39.778 [Finalizer] INFO catl.web.test.ThreadGcTest - finalize:弱引用
16:43:39.778 [Thread-7] INFO catl.web.test.ThreadGcTest - GC Test:软引用
16:43:39.778 [Thread-4] INFO catl.web.test.ThreadGcTest - GC Test:弱引用2
16:43:39.778 [Thread-5] INFO catl.web.test.ThreadGcTest - GC Test:弱引用
16:43:39.778 [Thread-6] INFO catl.web.test.ThreadGcTest - GC Test:强引用
16:43:39.782 [Finalizer] INFO catl.web.test.ThreadGcTest - finalize:虚引用
null