GC实例及参考(1)

  • java代码:
import java.lang.ref.WeakReference;
/**
 * Created by niewj on 2017/12/17.
 */
public class Main {
    public static void main(String[] args) {
        User a = new User();
        WeakReference b = new WeakReference(a);

        int i = 0;
        while (true) {
            if (b.get() != null) {
                i++;
                System.out.println("A还没被回收!!" + i);
            } else {
                System.out.println("A---被回收了----------");
                break;
            }
        }
    }
}

class User {
}
GC实例及参考(1)_第1张图片
不得不借用此神图了

1. UseSerialGC
(UseSerialGC > Serial + Serial Old)

XX:+UseSerialGC -XX:+PrintGC -XX:+PrintGCDetails

......
A还没被回收!!384384
A还没被回收!!384385
A还没被回收!!384386
[GC[DefNew: 68416K->812K(76928K), 0.0032924 secs] 68416K->812K(247872K), 0.0033418 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
A还没被回收!!384387
A---被回收了----------
Heap
def new generation total 76928K, used 2837K [0x0000000700800000, 0x0000000705b70000, 0x0000000753f50000)
eden space 68416K, 2% used [0x0000000700800000, 0x00000007009fa518, 0x0000000704ad0000)
from space 8512K, 9% used [0x0000000705320000, 0x00000007053eb178, 0x0000000705b70000)
to space 8512K, 0% used [0x0000000704ad0000, 0x0000000704ad0000, 0x0000000705320000)
tenured generation total 170944K, used 0K [0x0000000753f50000, 0x000000075e640000, 0x00000007fae00000)
the space 170944K, 0% used [0x0000000753f50000, 0x0000000753f50000, 0x0000000753f50200, 0x000000075e640000)
compacting perm gen total 21248K, used 3165K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)
the space 21248K, 14% used [0x00000007fae00000, 0x00000007fb117430, 0x00000007fb117600, 0x00000007fc2c0000)
No shared spaces configured.

2. UseParNew
(UseParNew > ParNew + Serial Old)

-XX:+UseParNewGC -XX:+PrintGC -XX:+PrintGCDetails

......
A还没被回收!!384386
[GC[ParNew: 68416K->837K(76928K), 0.0011650 secs] 68416K->837K(247872K), 0.0012095 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
A还没被回收!!384387
A---被回收了----------
Heap
par new generation total 76928K, used 3560K [0x0000000700800000, 0x0000000705b70000, 0x0000000753f50000)
eden space 68416K, 3% used [0x0000000700800000, 0x0000000700aa8cd8, 0x0000000704ad0000)
from space 8512K, 9% used [0x0000000705320000, 0x00000007053f1540, 0x0000000705b70000)
to space 8512K, 0% used [0x0000000704ad0000, 0x0000000704ad0000, 0x0000000705320000)
tenured generation total 170944K, used 0K [0x0000000753f50000, 0x000000075e640000, 0x00000007fae00000)
the space 170944K, 0% used [0x0000000753f50000, 0x0000000753f50000, 0x0000000753f50200, 0x000000075e640000)
compacting perm gen total 21248K, used 3165K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)
the space 21248K, 14% used [0x00000007fae00000, 0x00000007fb117430, 0x00000007fb117600, 0x00000007fc2c0000)
No shared spaces configured.

3. UseConcMarkSweepGC
(UseConcMarkSweepGC > ParNew + CMS + ..Serial Old)

-XX:+UseConcMarkSweepGC -XX:+PrintGC -XX:+PrintGCDetails
如果出现Concurrent Mode Failure, 老年代用 Serial Old 后备
-XX:+UseConcMarkSweepGC -XX:+PrintGC -XX:+PrintGCDetails

......
A还没被回收!!384385
A还没被回收!!384386
[GC[ParNew: 68416K->836K(76928K), 0.0013431 secs] 68416K->836K(247872K), 0.0014041 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
A还没被回收!!384387
A---被回收了----------
Heap
par new generation total 76928K, used 3559K [0x0000000700800000, 0x0000000705b70000, 0x00000007154c0000)
eden space 68416K, 3% used [0x0000000700800000, 0x0000000700aa8cd8, 0x0000000704ad0000)
from space 8512K, 9% used [0x0000000705320000, 0x00000007053f1130, 0x0000000705b70000)
to space 8512K, 0% used [0x0000000704ad0000, 0x0000000704ad0000, 0x0000000705320000)
concurrent mark-sweep generation total 170944K, used 0K [0x00000007154c0000, 0x000000071fbb0000, 0x00000007fae00000)
concurrent-mark-sweep perm gen total 21248K, used 3166K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)

4. UseParallelGC
(UseParallelGC > Parallel Scanvenge + Serial Old)

-XX:+UseParallelGC -XX:+PrintGC -XX:+PrintGCDetails

A还没被回收!!364573
A还没被回收!!364574
[GC [PSYoungGen: 65024K->962K(75264K)] 65024K->970K(245760K), 0.0012285 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
A还没被回收!!364575
A---被回收了----------
Heap
PSYoungGen total 75264K, used 3551K [0x00000007ac880000, 0x00000007b1c00000, 0x0000000800000000)
eden space 65024K, 3% used [0x00000007ac880000,0x00000007acb070d8,0x00000007b0800000)
from space 10240K, 9% used [0x00000007b0800000,0x00000007b08f0bd0,0x00000007b1200000)
to space 10240K, 0% used [0x00000007b1200000,0x00000007b1200000,0x00000007b1c00000)
ParOldGen total 170496K, used 8K [0x0000000705a00000, 0x0000000710080000, 0x00000007ac880000)
object space 170496K, 0% used [0x0000000705a00000,0x0000000705a02000,0x0000000710080000)
PSPermGen total 21504K, used 3165K [0x0000000700800000, 0x0000000701d00000, 0x0000000705a00000)
object space 21504K, 14% used [0x0000000700800000,0x0000000700b17430,0x0000000701d00000)

5. UseParallelOldGC
(UseParallelOldGC > Parallel Scavenge + Parallel Old)

-XX:+UseParallelOldGC -XX:+PrintGC -XX:+PrintGCDetails

......
A还没被回收!!364574
A还没被回收!!364575
[GC [PSYoungGen: 65024K->874K(75264K)] 65024K->882K(245760K), 0.0020625 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
A还没被回收!!364576
A---被回收了----------
Heap
PSYoungGen total 75264K, used 3463K [0x00000007ac880000, 0x00000007b1c00000, 0x0000000800000000)
eden space 65024K, 3% used [0x00000007ac880000,0x00000007acb070d8,0x00000007b0800000)
from space 10240K, 8% used [0x00000007b0800000,0x00000007b08dabc0,0x00000007b1200000)
to space 10240K, 0% used [0x00000007b1200000,0x00000007b1200000,0x00000007b1c00000)
ParOldGen total 170496K, used 8K [0x0000000705a00000, 0x0000000710080000, 0x00000007ac880000)
object space 170496K, 0% used [0x0000000705a00000,0x0000000705a02000,0x0000000710080000)
PSPermGen total 21504K, used 3165K [0x0000000700800000, 0x0000000701d00000, 0x0000000705a00000)
object space 21504K, 14% used [0x0000000700800000,0x0000000700b17430,0x0000000701d00000)

6. UseG1GC
UseG1GC

-XX:+UseG1GC -XX:+PrintGC -XX:+PrintGCDetails

......
A还没被回收!!71007
A还没被回收!!71008
A还没被回收!!71009[GC pause (young), 0.0012627 secs]
[Parallel Time: 1.0 ms, GC Workers: 4]
[GC Worker Start (ms): Min: 922.7, Avg: 922.8, Max: 922.8, Diff: 0.0]
[Ext Root Scanning (ms): Min: 0.4, Avg: 0.4, Max: 0.5, Diff: 0.1, Sum: 1.8]
[Update RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
[Processed Buffers: Min: 0, Avg: 2.8, Max: 11, Diff: 11, Sum: 11]
[Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
[Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
[Object Copy (ms): Min: 0.5, Avg: 0.5, Max: 0.5, Diff: 0.1, Sum: 2.0]
[Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
[GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.1]
[GC Worker Total (ms): Min: 1.0, Avg: 1.0, Max: 1.0, Diff: 0.0, Sum: 3.9]
[GC Worker End (ms): Min: 923.7, Avg: 923.7, Max: 923.7, Diff: 0.0]
[Code Root Fixup: 0.0 ms]
[Code Root Migration: 0.0 ms]
[Clear CT: 0.0 ms]
[Other: 0.2 ms]
[Choose CSet: 0.0 ms]
[Ref Proc: 0.1 ms]
[Ref Enq: 0.0 ms]
[Free CSet: 0.0 ms]
[Eden: 14.0M(14.0M)->0.0B(20.0M) Survivors: 0.0B->1024.0K Heap: 14.0M(251.0M)->896.1K(251.0M)]
[Times: user=0.00 sys=0.00, real=0.00 secs]

A---被回收了----------
Heap
garbage-first heap total 257024K, used 896K [0x0000000700800000, 0x0000000710300000, 0x00000007fae00000)
region size 1024K, 2 young (2048K), 1 survivors (1024K)
compacting perm gen total 20480K, used 3164K [0x00000007fae00000, 0x00000007fc200000, 0x0000000800000000)
the space 20480K, 15% used [0x00000007fae00000, 0x00000007fb117388, 0x00000007fb117400, 0x00000007fc200000)
No shared spaces configured.

相关文章课关联参考
Java GC系列(1):Java垃圾回收简介
Java GC系列(2):Java垃圾回收是如何工作的?
Java GC系列(3):垃圾回收器种类
Java GC系列(4):垃圾回收监视和分析

你可能感兴趣的:(GC实例及参考(1))