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<User> b = new WeakReference<User>(a);

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

class User {
}
不得不借用此神图了

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):垃圾回收监视和分析

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容