简单GC具体操作参数查看

代码:

public class HeapTest {
    private static final int _1M = 1024 * 1024;

    public static void main(String[] args) throws InterruptedException {
        System.gc();
        byte[] byte1 = new byte[2 * _1M];
        byte[] byte2 = new byte[2 * _1M];
        byte[] byte3 = new byte[2 * _1M];
        byte[] byte4 = new byte[2 * _1M];
        byte[] byte5 = new byte[2 * _1M];
        byte[] byte6 = new byte[5 * _1M];

        byte[] byte7 = new byte[2 * _1M];

        System.out.println("asdf");
    }
}

注意byte6是5M

vm参数:

-Xms20m
-Xmx20m
-Xmn10m
-verbose:gc
-XX:+PrintGCDetails
-XX:+PrintTenuringDistribution
-XX:+PrintGCTimeStamps

分别打上断点,使用

$ jmap -heap 17432

System.gc

1.049: [GC (System.gc()) 
Desired survivor size 1048576 bytes, new threshold 7 (max 15)
[PSYoungGen: 6657K->1000K(9216K)] 6657K->1337K(19456K), 0.0015197 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
1.051: [Full GC (System.gc()) [PSYoungGen: 1000K->0K(9216K)] [ParOldGen: 337K->1290K(10240K)] 1337K->1290K(19456K), [Metaspace: 3777K->3777K(1056768K)], 0.0063289 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 

GC日志分析

Young GC日志:

image

Full GC日志:

image

堆信息:

查看堆信息:

$ jps  -l
11120 com.qhong.gc.HeapTest
11652 sun.tools.jps.Jps
18708 com.shitou.Application
11352
18200 org.jetbrains.jps.cmdline.Launcher
13644 org.jetbrains.jps.cmdline.Launcher

jmap:

$ jmap -heap 11120
Attaching to process ID 11120, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.201-b09

using thread-local object allocation.
Parallel GC with 4 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0                
   MaxHeapFreeRatio         = 100         #GC后如果发现空闲堆内存占到整个预估堆内存的N%(百分比)  
   MaxHeapSize              = 20971520 (20.0MB)    # 堆最大空闲    jvm参数 -Xms20m
   NewSize                  = 10485760 (10.0MB)    # 年轻代空间    jvm参数 -Xmn10m
   MaxNewSize               = 10485760 (10.0MB)    # 年轻代最大空间
   OldSize                  = 10485760 (10.0MB)    # 老年代空间 =(等于)堆内存大小 -(减去)年轻代大小
   NewRatio                 = 2   
   SurvivorRatio            = 8   # 年轻代内存又被分成三部分 Eden 空间 80% 而From Survivor 空间 和 To Survivor空间 分别占用10%
   MetaspaceSize            = 21807104 (20.796875MB) # 设置元空间的最大值 jvm参数 -XX:MaxMetaspaceSize
   CompressedClassSpaceSize = 1073741824 (1024.0MB)  # 类指针压缩空间大小, 默认为1G
   MaxMetaspaceSize         = 17592186044415 MB    # 是分配给类元数据空间的最大值
   G1HeapRegionSize         = 0 (0.0MB) # G1区块的大小, 取值为1M至32M. 其取值是要根据最小Heap大小划分出2048个区块

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 8388608 (8.0MB)
   used     = 167792 (0.1600189208984375MB)
   free     = 8220816 (7.8399810791015625MB)
   2.0002365112304688% used
From Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
To Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
PS Old Generation
   capacity = 10485760 (10.0MB)
   used     = 1321344 (1.2601318359375MB)
   free     = 9164416 (8.7398681640625MB)
   12.601318359375% used

2072 interned Strings occupying 186064 bytes.

byte1执行后

Eden Space:
   capacity = 8388608 (8.0MB)
   used     = 2428440 (2.3159408569335938MB)
   free     = 5960168 (5.684059143066406MB)
   28.949260711669922% used
From Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
To Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
PS Old Generation
   capacity = 10485760 (10.0MB)
   used     = 1321480 (1.2602615356445312MB)
   free     = 9164280 (8.739738464355469MB)
   12.602615356445312% used

byte2执行后

Eden Space:
   capacity = 8388608 (8.0MB)
   used     = 4525528 (4.315879821777344MB)
   free     = 3863080 (3.6841201782226562MB)
   53.9484977722168% used
From Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
To Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
PS Old Generation
   capacity = 10485760 (10.0MB)
   used     = 1321344 (1.2601318359375MB)
   free     = 9164416 (8.7398681640625MB)
   12.601318359375% used

byte3执行后

Eden Space:
   capacity = 8388608 (8.0MB)
   used     = 6622696 (6.315895080566406MB)
   free     = 1765912 (1.6841049194335938MB)
   78.94868850708008% used
From Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
To Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
PS Old Generation
   capacity = 10485760 (10.0MB)
   used     = 1321344 (1.2601318359375MB)
   free     = 9164416 (8.7398681640625MB)
   12.601318359375% used

byte4执行后

gc:

247.099: [GC (Allocation Failure) 
Desired survivor size 1048576 bytes, new threshold 7 (max 15)
[PSYoungGen: 6467K->0K(9216K)] 7757K->7434K(19456K), 0.0041433 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
247.104: [Full GC (Ergonomics) [PSYoungGen: 0K->0K(9216K)] [ParOldGen: 7434K->7109K(10240K)] 7434K->7109K(19456K), [Metaspace: 3769K->3769K(1056768K)], 0.0083147 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 

heap:

Eden Space:
   capacity = 8388608 (8.0MB)
   used     = 2181072 (2.0800323486328125MB)
   free     = 6207536 (5.9199676513671875MB)
   26.000404357910156% used
From Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
To Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
PS Old Generation
   capacity = 10485760 (10.0MB)
   used     = 7280080 (6.9428253173828125MB)
   free     = 3205680 (3.0571746826171875MB)
   69.42825317382812% used

byte5执行后

Eden Space:
   capacity = 8388608 (8.0MB)
   used     = 4421640 (4.216804504394531MB)
   free     = 3966968 (3.7831954956054688MB)
   52.71005630493164% used
From Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
To Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
PS Old Generation
   capacity = 10485760 (10.0MB)
   used     = 7280080 (6.9428253173828125MB)
   free     = 3205680 (3.0571746826171875MB)
   69.42825317382812% used

byte6执行后

GC:

337.887: [Full GC (Ergonomics) [PSYoungGen: 4318K->2048K(9216K)] [ParOldGen: 7109K->9157K(10240K)] 11427K->11205K(19456K), [Metaspace: 3769K->3769K(1056768K)], 0.0064432 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 

Heap:

Eden Space:
   capacity = 8388608 (8.0MB)
   used     = 7340064 (7.000030517578125MB)
   free     = 1048544 (0.999969482421875MB)
   87.50038146972656% used
From Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
To Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
PS Old Generation
   capacity = 10485760 (10.0MB)
   used     = 9377000 (8.942604064941406MB)
   free     = 1108760 (1.0573959350585938MB)
   89.42604064941406% used

byte7执行后

370.908: [Full GC (Ergonomics) [PSYoungGen: 7168K->7168K(9216K)] [ParOldGen: 9157K->9157K(10240K)] 16325K->16325K(19456K), [Metaspace: 3769K->3769K(1056768K)], 0.0045883 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
370.913: [Full GC (Allocation Failure) [PSYoungGen: 7168K->7168K(9216K)] [ParOldGen: 9157K->9157K(10240K)] 16325K->16325K(19456K), [Metaspace: 3769K->3769K(1056768K)], 0.0028624 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at com.qhong.gc.HeapTest.main(HeapTest.java:18)
Heap
 PSYoungGen      total 9216K, used 7368K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 89% used [0x00000000ff600000,0x00000000ffd32238,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 9157K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 89% used [0x00000000fec00000,0x00000000ff4f14e8,0x00000000ff600000)
 Metaspace       used 3799K, capacity 4596K, committed 4864K, reserved 1056768K
  class space    used 414K, capacity 432K, committed 512K, reserved 1048576K
Disconnected from the target VM, address: '127.0.0.1:6560', transport: 'socket'

发现一开始eden区就不是空的

如果一开始不执行System.gc(),那么除了eden区,其他区都是空的,包括老年代也是空的

参考:

《深入理解Java虚拟机》(六)堆内存使用分析,垃圾收集器 GC 日志解读

Jvm 系列(五):Java GC 分析

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

推荐阅读更多精彩内容