简单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 分析

你可能感兴趣的:(简单GC具体操作参数查看)