代碼:
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參數(shù):
-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后如果發(fā)現(xiàn)空閑堆內(nèi)存占到整個預(yù)估堆內(nèi)存的N%(百分比)
MaxHeapSize = 20971520 (20.0MB) # 堆最大空閑 jvm參數(shù) -Xms20m
NewSize = 10485760 (10.0MB) # 年輕代空間 jvm參數(shù) -Xmn10m
MaxNewSize = 10485760 (10.0MB) # 年輕代最大空間
OldSize = 10485760 (10.0MB) # 老年代空間 =(等于)堆內(nèi)存大小 -(減去)年輕代大小
NewRatio = 2
SurvivorRatio = 8 # 年輕代內(nèi)存又被分成三部分 Eden 空間 80% 而From Survivor 空間 和 To Survivor空間 分別占用10%
MetaspaceSize = 21807104 (20.796875MB) # 設(shè)置元空間的最大值 jvm參數(shù) -XX:MaxMetaspaceSize
CompressedClassSpaceSize = 1073741824 (1024.0MB) # 類指針壓縮空間大小迎献, 默認(rèn)為1G
MaxMetaspaceSize = 17592186044415 MB # 是分配給類元數(shù)據(jù)空間的最大值
G1HeapRegionSize = 0 (0.0MB) # G1區(qū)塊的大小只嚣, 取值為1M至32M. 其取值是要根據(jù)最小Heap大小劃分出2048個區(qū)塊
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執(zhí)行后
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執(zhí)行后
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執(zhí)行后
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執(zhí)行后
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執(zhí)行后
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執(zhí)行后
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執(zhí)行后
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'
發(fā)現(xiàn)一開始eden區(qū)就不是空的
如果一開始不執(zhí)行System.gc(),那么除了eden區(qū),其他區(qū)都是空的洛波,包括老年代也是空的