1.創(chuàng)建SpringBoot項(xiàng)目
通過(guò)一個(gè)死循環(huán),讓內(nèi)存溢出
@RequestMapping("/oomTest")
public String oomTest() {
int i = 0;
while (true) {
integerList.add(i);
i++;
System.out.println(i);
}
}
2.記得修改JVM參數(shù)
既要設(shè)置內(nèi)存大小,也要在OOM時(shí)保存文件,也要保存GC日志
-Xms40m
-Xmx40m
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=./
-XX:+PrintGCDetails
-Xloggc:./gc.log
3.啟動(dòng)項(xiàng)目,調(diào)用接口
循環(huán)到一定程度后,OOM發(fā)生了:

image.png
4.在根目錄找到.hprof文件和gc.log文件
4.1使用MAT分析.hprof文件

image.png
4.2 分析gc.log文件
Java HotSpot(TM) 64-Bit Server VM (25.111-b14) for windows-amd64 JRE (1.8.0_111-b14), built on Sep 22 2016 19:24:05 by "java_re" with MS VC++ 10.0 (VS2010)
Memory: 4k page, physical 33279588k(17434308k free), swap 38260324k(17228608k free)
CommandLine flags: -XX:-BytecodeVerificationLocal -XX:-BytecodeVerificationRemote -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./ -XX:InitialHeapSize=41943040 -XX:+ManagementServer -XX:MaxHeapSize=41943040 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:TieredStopAtLevel=1 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
0.621: [GC (Allocation Failure) [PSYoungGen: 10240K->1528K(11776K)] 10240K->2061K(39424K), 0.0017762 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.733: [GC (Allocation Failure) [PSYoungGen: 11768K->1528K(11776K)] 12301K->3549K(39424K), 0.0022136 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.798: [GC (Allocation Failure) [PSYoungGen: 11768K->1528K(11776K)] 13789K->4684K(39424K), 0.0018205 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.835: [GC (Allocation Failure) [PSYoungGen: 11768K->1528K(11776K)] 14924K->5923K(39424K), 0.0019339 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.870: [GC (Allocation Failure) [PSYoungGen: 11768K->1512K(11776K)] 16163K->7076K(39424K), 0.0016447 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.906: [GC (Allocation Failure) [PSYoungGen: 11752K->1528K(7680K)] 17316K->7919K(35328K), 0.0019457 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.928: [GC (GCLocker Initiated GC) [PSYoungGen: 7672K->1408K(9728K)] 14064K->8673K(37376K), 0.0016146 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.950: [GC (Allocation Failure) [PSYoungGen: 7552K->1216K(9728K)] 14821K->8913K(37376K), 0.0011682 secs] [Times: user=0.14 sys=0.00, real=0.00 secs]
0.983: [GC (Allocation Failure) [PSYoungGen: 7360K->1306K(9728K)] 15057K->9284K(37376K), 0.0011392 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.015: [GC (Allocation Failure) [PSYoungGen: 7443K->1450K(9728K)] 15421K->9796K(37376K), 0.0040677 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.047: [GC (Allocation Failure) [PSYoungGen: 7594K->1048K(9728K)] 15940K->9912K(37376K), 0.0017552 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.077: [GC (Allocation Failure) [PSYoungGen: 7192K->995K(9728K)] 16056K->10035K(37376K), 0.0012529 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.106: [GC (Allocation Failure) [PSYoungGen: 7139K->1046K(9728K)] 16179K->10246K(37376K), 0.0014971 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.132: [GC (Allocation Failure) [PSYoungGen: 7190K->988K(9728K)] 16390K->10372K(37376K), 0.0012089 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.152: [GC (Metadata GC Threshold) [PSYoungGen: 4429K->562K(9728K)] 13813K->10167K(37376K), 0.0013494 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.153: [Full GC (Metadata GC Threshold) [PSYoungGen: 562K->0K(9728K)] [ParOldGen: 9604K->5712K(27648K)] 10167K->5712K(37376K), [Metaspace: 20603K->20603K(1067008K)], 0.0402913 secs] [Times: user=0.22 sys=0.00, real=0.04 secs]
1.236: [GC (Allocation Failure) [PSYoungGen: 6144K->1161K(9728K)] 11856K->6945K(37376K), 0.0015287 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.255: [GC (Allocation Failure) [PSYoungGen: 7291K->783K(9728K)] 13075K->7183K(37376K), 0.0010509 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.267: [GC (Allocation Failure) [PSYoungGen: 6927K->740K(9728K)] 13327K->7500K(37376K), 0.0009120 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.291: [GC (Allocation Failure) [PSYoungGen: 6828K->1789K(9728K)] 13588K->8830K(37376K), 0.0014743 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.314: [GC (Allocation Failure) [PSYoungGen: 7933K->2577K(9728K)] 14974K->11103K(37376K), 0.0015567 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.346: [GC (Allocation Failure) [PSYoungGen: 8721K->925K(9728K)] 17247K->11795K(37376K), 0.0012903 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.371: [GC (Allocation Failure) [PSYoungGen: 7069K->704K(9728K)] 17939K->12044K(37376K), 0.0009646 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.401: [GC (Allocation Failure) [PSYoungGen: 6848K->671K(9728K)] 18188K->12324K(37376K), 0.0010683 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.435: [GC (Allocation Failure) [PSYoungGen: 6815K->956K(9728K)] 18468K->12881K(37376K), 0.0013447 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.466: [GC (Allocation Failure) [PSYoungGen: 7100K->1004K(9728K)] 19025K->13096K(37376K), 0.0013478 secs] [Times: user=0.06 sys=0.00, real=0.00 secs]
1.493: [GC (Allocation Failure) [PSYoungGen: 7146K->777K(10240K)] 19238K->13174K(37888K), 0.0012582 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.533: [GC (Allocation Failure) [PSYoungGen: 7945K->881K(10240K)] 20342K->13565K(37888K), 0.0010946 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.563: [GC (Allocation Failure) [PSYoungGen: 8049K->672K(10240K)] 20733K->13602K(37888K), 0.0012861 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.593: [GC (Allocation Failure) [PSYoungGen: 7840K->672K(10240K)] 20770K->13998K(37888K), 0.0012785 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.620: [GC (Allocation Failure) [PSYoungGen: 7840K->562K(10240K)] 21166K->14321K(37888K), 0.0013071 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.650: [GC (Allocation Failure) [PSYoungGen: 7730K->640K(10240K)] 21489K->14662K(37888K), 0.0013652 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.680: [GC (Allocation Failure) [PSYoungGen: 7808K->910K(10752K)] 21830K->15253K(38400K), 0.0010506 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.723: [GC (Allocation Failure) [PSYoungGen: 9102K->736K(10752K)] 23445K->15590K(38400K), 0.0020021 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.761: [GC (Allocation Failure) [PSYoungGen: 8928K->967K(10752K)] 23782K->16261K(38400K), 0.0013968 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.803: [GC (Allocation Failure) [PSYoungGen: 9159K->1524K(10752K)] 24453K->17170K(38400K), 0.0013322 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.834: [GC (Allocation Failure) [PSYoungGen: 9715K->774K(10752K)] 25362K->16996K(38400K), 0.0016574 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.878: [GC (Allocation Failure) [PSYoungGen: 8966K->994K(10752K)] 25188K->17585K(38400K), 0.0016544 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.915: [GC (Allocation Failure) [PSYoungGen: 9186K->576K(11264K)] 25777K->17552K(38912K), 0.0016180 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.954: [GC (Allocation Failure) [PSYoungGen: 9792K->674K(11264K)] 26768K->18066K(38912K), 0.0013063 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.985: [GC (Allocation Failure) [PSYoungGen: 9890K->672K(11264K)] 27282K->18400K(38912K), 0.0017008 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2.021: [GC (Allocation Failure) [PSYoungGen: 9888K->1189K(11264K)] 27616K->19389K(38912K), 0.0015143 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2.055: [GC (Allocation Failure) [PSYoungGen: 10405K->672K(11264K)] 28605K->19600K(38912K), 0.0015006 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2.089: [GC (Allocation Failure) [PSYoungGen: 9888K->672K(11264K)] 28816K->20048K(38912K), 0.0011516 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2.124: [GC (Allocation Failure) [PSYoungGen: 9888K->705K(11264K)] 29264K->20361K(38912K), 0.0018830 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2.157: [GC (Allocation Failure) [PSYoungGen: 9921K->1043K(11264K)] 29577K->21123K(38912K), 0.0013440 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2.190: [GC (Allocation Failure) [PSYoungGen: 10259K->784K(11264K)] 30339K->21288K(38912K), 0.0014076 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2.218: [GC (Allocation Failure) [PSYoungGen: 10000K->576K(11264K)] 30504K->21528K(38912K), 0.0015571 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2.251: [GC (Allocation Failure) [PSYoungGen: 9792K->942K(11264K)] 30744K->22262K(38912K), 0.0015738 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2.283: [GC (Allocation Failure) [PSYoungGen: 10158K->832K(11264K)] 31478K->22640K(38912K), 0.0016733 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2.314: [GC (Allocation Failure) [PSYoungGen: 10048K->787K(11776K)] 31856K->23147K(39424K), 0.0017683 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2.340: [GC (Allocation Failure) [PSYoungGen: 11027K->576K(11776K)] 33387K->23328K(39424K), 0.0013116 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
2.366: [GC (Allocation Failure) [PSYoungGen: 10816K->634K(11776K)] 33568K->23826K(39424K), 0.0013396 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
13.719: [GC (Allocation Failure) [PSYoungGen: 10874K->1295K(11776K)] 34066K->24815K(39424K), 0.0024640 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
13.864: [GC (Allocation Failure) [PSYoungGen: 11535K->1373K(10752K)] 35055K->25441K(38400K), 0.0022676 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
14.046: [GC (Allocation Failure) [PSYoungGen: 10589K->1376K(11264K)] 34657K->26229K(38912K), 0.0031012 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
14.249: [GC (Allocation Failure) [PSYoungGen: 10592K->2033K(10240K)] 35445K->27149K(37888K), 0.0028242 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
14.423: [GC (Allocation Failure) [PSYoungGen: 10225K->1801K(10752K)] 35341K->28366K(38400K), 0.0040072 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
14.427: [Full GC (Ergonomics) [PSYoungGen: 1801K->0K(10752K)] [ParOldGen: 26564K->20847K(27648K)] 28366K->20847K(38400K), [Metaspace: 33268K->33268K(1079296K)], 0.1311243 secs] [Times: user=0.72 sys=0.00, real=0.13 secs]
14.736: [GC (Allocation Failure) [PSYoungGen: 8192K->928K(10752K)] 29039K->21775K(38400K), 0.0079679 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
14.883: [GC (Allocation Failure) [PSYoungGen: 9120K->2558K(10752K)] 29967K->23717K(38400K), 0.0050739 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
15.347: [GC (Allocation Failure) [PSYoungGen: 10750K->928K(9728K)] 31909K->24685K(37376K), 0.0042086 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
15.473: [GC (Allocation Failure) [PSYoungGen: 6823K->768K(10240K)] 30581K->25341K(37888K), 0.0123800 secs] [Times: user=0.16 sys=0.00, real=0.01 secs]
15.608: [GC (Allocation Failure) [PSYoungGen: 7936K->2686K(10240K)] 32509K->27852K(37888K), 0.0047171 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
15.792: [GC (Allocation Failure) --[PSYoungGen: 9854K->9854K(10240K)] 35020K->37498K(37888K), 0.0112540 secs] [Times: user=0.14 sys=0.00, real=0.01 secs]
15.803: [Full GC (Ergonomics) [PSYoungGen: 9854K->0K(10240K)] [ParOldGen: 27644K->25779K(27648K)] 37498K->25779K(37888K), [Metaspace: 33268K->33268K(1079296K)], 0.2247909 secs] [Times: user=1.06 sys=0.01, real=0.22 secs]
16.180: [GC (Allocation Failure) [PSYoungGen: 7168K->800K(10240K)] 32947K->26579K(37888K), 0.0068336 secs] [Times: user=0.16 sys=0.00, real=0.01 secs]
16.334: [GC (Allocation Failure) [PSYoungGen: 7968K->1504K(10240K)] 33747K->27283K(37888K), 0.0118211 secs] [Times: user=0.14 sys=0.00, real=0.01 secs]
16.437: [GC (Allocation Failure) --[PSYoungGen: 8672K->8672K(10240K)] 34451K->34451K(37888K), 0.0141972 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
16.452: [Full GC (Ergonomics) [PSYoungGen: 8672K->0K(10240K)] [ParOldGen: 25779K->26133K(27648K)] 34451K->26133K(37888K), [Metaspace: 33268K->33046K(1079296K)], 0.1953328 secs] [Times: user=0.88 sys=0.02, real=0.19 secs]
16.795: [Full GC (Ergonomics) [PSYoungGen: 7168K->0K(10240K)] [ParOldGen: 26133K->26792K(27648K)] 33301K->26792K(37888K), [Metaspace: 33046K->33046K(1079296K)], 0.0963588 secs] [Times: user=0.50 sys=0.00, real=0.10 secs]
17.039: [Full GC (Ergonomics) [PSYoungGen: 7168K->0K(10240K)] [ParOldGen: 26792K->27475K(27648K)] 33960K->27475K(37888K), [Metaspace: 33046K->33046K(1079296K)], 0.0731456 secs] [Times: user=0.61 sys=0.00, real=0.07 secs]
17.261: [Full GC (Ergonomics) [PSYoungGen: 7168K->536K(10240K)] [ParOldGen: 27475K->27621K(27648K)] 34643K->28158K(37888K), [Metaspace: 33046K->33046K(1079296K)], 0.0792916 secs] [Times: user=0.63 sys=0.02, real=0.08 secs]
17.481: [Full GC (Ergonomics) [PSYoungGen: 7168K->1166K(10240K)] [ParOldGen: 27621K->27621K(27648K)] 34789K->28788K(37888K), [Metaspace: 33046K->33046K(1079296K)], 0.0822999 secs] [Times: user=0.81 sys=0.00, real=0.08 secs]
17.690: [Full GC (Ergonomics) [PSYoungGen: 7168K->1738K(10240K)] [ParOldGen: 27621K->27621K(27648K)] 34789K->29360K(37888K), [Metaspace: 33046K->33046K(1079296K)], 0.1052190 secs] [Times: user=0.73 sys=0.02, real=0.11 secs]
17.919: [Full GC (Ergonomics) [PSYoungGen: 7168K->2253K(10240K)] [ParOldGen: 27621K->27621K(27648K)] 34789K->29875K(37888K), [Metaspace: 33046K->33046K(1079296K)], 0.1066049 secs] [Times: user=0.78 sys=0.00, real=0.11 secs]
18.114: [Full GC (Ergonomics) [PSYoungGen: 6179K->2621K(10240K)] [ParOldGen: 27621K->27621K(27648K)] 33801K->30243K(37888K), [Metaspace: 33046K->33046K(1079296K)], 0.1012066 secs] [Times: user=0.88 sys=0.02, real=0.10 secs]
18.215: [Full GC (Allocation Failure) [PSYoungGen: 2621K->2621K(10240K)] [ParOldGen: 27621K->27555K(27648K)] 30243K->30176K(37888K), [Metaspace: 33046K->33036K(1079296K)], 0.2175771 secs] [Times: user=1.47 sys=0.00, real=0.22 secs]
18.693: [Full GC (Ergonomics) [PSYoungGen: 7168K->3177K(10240K)] [ParOldGen: 27555K->27541K(27648K)] 34723K->30718K(37888K), [Metaspace: 33481K->33481K(1079296K)], 0.2012172 secs] [Times: user=1.42 sys=0.01, real=0.20 secs]
5297.358: [Full GC (Ergonomics) [PSYoungGen: 7168K->3360K(10240K)] [ParOldGen: 27541K->27518K(27648K)] 34709K->30879K(37888K), [Metaspace: 33671K->33671K(1079296K)], 0.1424810 secs] [Times: user=0.83 sys=0.00, real=0.14 secs]
7009.462: [Full GC (Ergonomics) [PSYoungGen: 4120K->3117K(10240K)] [ParOldGen: 27518K->27513K(27648K)] 31639K->30631K(37888K), [Metaspace: 33687K->33685K(1079296K)], 0.1384130 secs] [Times: user=0.92 sys=0.02, real=0.14 secs]
7009.601: [Full GC (Allocation Failure) [PSYoungGen: 3117K->3115K(10240K)] [ParOldGen: 27513K->27513K(27648K)] 30631K->30629K(37888K), [Metaspace: 33685K->33685K(1079296K)], 0.0834903 secs] [Times: user=0.55 sys=0.00, real=0.08 secs]
5.如何讀GC文件
其中GC, Full GC 說(shuō)明垃圾收集的停頓類型,full GC表示其他線程均停止工作,僅有垃圾回收線程工作
其中 PSYoungGen, ParOldGen 表示 GC發(fā)生的區(qū)域,該區(qū)域與 使用的GC收集器密切相關(guān)。
- 例 PSYoungGen 表示 采用 Parallel Scavenge收集器時(shí), 新生代發(fā)生的變化
- 同理 當(dāng)出現(xiàn) DefNew 表示 采用 Serial收集器時(shí),新生代發(fā)生的變化, default new generation。
方括號(hào)內(nèi)數(shù)字:7307K->480K(9216K) 表示 GC前該內(nèi)存區(qū)域已使用的容量 -> GC后該內(nèi)存區(qū)域已使用的容量(該內(nèi)存區(qū)域總?cè)萘浚?方括號(hào)外數(shù)字:7307K->6624K(19456K) 表示 GC前java 堆已使用容量 -> GC后java堆已使用容量(java堆總?cè)萘浚?0.0072860 secs 表示 該內(nèi)存區(qū)域GC所占用的時(shí)間
[Times: user=0.01 sys=0.01, real=0.00 secs] :
- 分別表示用戶態(tài)消耗cpu時(shí)間,
- 內(nèi)核態(tài)消耗cpu時(shí)間,
- 操作從開(kāi)始到結(jié)束經(jīng)過(guò)的墻鐘時(shí)間
cpu時(shí)間與墻鐘時(shí)間區(qū)別:
- 墻鐘時(shí)間包括非運(yùn)算等待耗時(shí),例如等待磁盤(pán)I/O,等待阻塞,而cpu不包括
- 但當(dāng)系統(tǒng)有多cpu或者多核的話,多線程操作會(huì)疊加這些cpu時(shí)間,所以看到user或sys時(shí)間超過(guò)real時(shí)間是完全正常的
6.常見(jiàn)報(bào)錯(cuò)解釋
GC (Allocation Failure)
Full GC (Allocation Failure)
- 表明本次引起GC的原因是因?yàn)樵谀贻p代中沒(méi)有足夠的空間能夠存儲(chǔ)新的數(shù)據(jù)了
GC (Metadata GC Threshold)
Full GC (Metadata GC Threshold)
- Metaspace區(qū)是保存在本地內(nèi)存中,是沒(méi)有上限的,
JDK8中,XX:MaxMetaspaceSize確實(shí)是沒(méi)有上限的,最大容量與機(jī)器的內(nèi)存有關(guān);
但是XX:MetaspaceSize是有一個(gè)默認(rèn)值的:21M,需要改大一點(diǎn);
Full GC (Ergonomics)
- 自動(dòng)選擇和調(diào)優(yōu)引發(fā)的FullGC
java.lang.OutOfMemoryError: GC overhead limit exceeded
- JVM花費(fèi)了98%的時(shí)間進(jìn)行垃圾回收,而只得到2%可用的內(nèi)存,頻繁的進(jìn)行內(nèi)存回收(最起碼已經(jīng)進(jìn)行了5次連續(xù)的垃圾回收),
JVM就會(huì)曝出ava.lang.OutOfMemoryError: GC overhead limit exceeded錯(cuò)誤
7.問(wèn)題
元空間是啥?
方法區(qū)也是所有線程共享。主要用于存儲(chǔ)類的信息、常量池、方法數(shù)據(jù)、方法代碼等。
方法區(qū)是JVM 的規(guī)范,永久代(PermGen space)是HotSpot對(duì)這種規(guī)范的實(shí)現(xiàn)。
在 JDK 1.8 中, HotSpot 已經(jīng)沒(méi)有 “PermGen space”這個(gè)區(qū)間了,取而代之的是 Metaspace(元空間)。
元空間的本質(zhì)和永久代類似,都是對(duì)JVM規(guī)范中方法區(qū)的實(shí)現(xiàn)。
不過(guò)元空間與永久代之間最大的區(qū)別在于:元空間并不在虛擬機(jī)中,而是使用本地內(nèi)存。
因此,默認(rèn)情況下,元空間的大小僅受本地內(nèi)存限制,但可以通過(guò)以下參數(shù)來(lái)指定元空間的大?。?-XX:MetaspaceSize 初始空間大小,達(dá)到該值就會(huì)觸發(fā)垃圾收集進(jìn)行類型卸載,同時(shí)GC會(huì)對(duì)該值進(jìn)行調(diào)整:
如果釋放了大量的空間,就適當(dāng)降低該值;
如果釋放了很少的空間,那么在不超過(guò)MaxMetaspaceSize時(shí),適當(dāng)提高該值。
-XX:MaxMetaspaceSize最大空間,默認(rèn)是沒(méi)有限制的。
除了上面兩個(gè)指定大小的選項(xiàng)以外,還有兩個(gè)與 GC 相關(guān)的屬性:
-XX:MinMetaspaceFreeRatio在GC之后,最小的Metaspace剩余空間容量的百分比,減少為分配空間所導(dǎo)致的垃圾收集
-XX:MaxMetaspaceFreeRatio在GC之后,最大的Metaspace剩余空間容量的百分比,減少為釋放空間所導(dǎo)致的垃圾收集
Young GC和Full GC分別在什么情況下會(huì)發(fā)生?
Young GC的觸發(fā)時(shí)機(jī):Young GC其實(shí)一般就是在新生代的Eden區(qū)域滿了之后就會(huì)觸發(fā),采用復(fù)制算法來(lái)回收新生代的垃圾。
Full GC的觸發(fā)時(shí)機(jī)如下:
(1)發(fā)生Young GC之前進(jìn)行檢查(允許空間分配擔(dān)保失?。?,如果“老年代可用的連續(xù)內(nèi)存空間” < “新生代歷次Young GC后升入老年代的對(duì)象總和的平均大小”,
說(shuō)明本次Young GC后可能升入老年代的對(duì)象大小,可能超過(guò)了老年代當(dāng)前可用內(nèi)存空間此時(shí)必須先觸發(fā)一次Old GC給老年代騰出更多的空間,然后再執(zhí)行Young GC。
(2)執(zhí)行Young GC之后有一批對(duì)象需要放入老年代,此時(shí)老年代就是沒(méi)有足夠的內(nèi)存空間存放這些對(duì)象了,此時(shí)必須立即觸發(fā)一次Old GC。
(3)老年代內(nèi)存使用率超過(guò)了92%,也要直接觸發(fā)Old GC,當(dāng)然這個(gè)比例是可以通過(guò)參數(shù)調(diào)整的。
概括成一句話,就是老年代空間也不夠了,沒(méi)法放入更多對(duì)象了,這個(gè)時(shí)候務(wù)必執(zhí)行Old GC對(duì)老年代進(jìn)行垃圾回收。
8.經(jīng)典案例
記一次詭異的頻繁Full GChttp://m.itdecent.cn/p/f3fd1664f1ee
一次由于YoungGC引起的性能問(wèn)題分析https://blog.csdn.net/zhxdick/article/details/79105421
9.JVM常用配置
堆設(shè)置
-Xms256M:初始堆大小256M,默認(rèn)為物理內(nèi)存的1/64
-Xmx1024M:最大堆大小1024M,默認(rèn)為物理內(nèi)存的1/4,等于與-XX:MaxHeapSize=64M
-Xmn64M:年輕代大小為64M(JDK1.4后支持),相當(dāng)于同時(shí)設(shè)置NewSize和MaxNewSize為64M
-XX:NewSize=64M:初始年輕代大小
-XX:MaxNewSize=256M:最大年輕代大小(默認(rèn)為堆最大值的1/3)
-XX:OldSize=64M:年老代大小64M(測(cè)試驗(yàn)證JDK1.8.191該參數(shù)設(shè)置無(wú)效,JDK11下設(shè)置成功)
-XX:NewRatio=4:年老代:年輕代=4:1
-XX:SurvivorRatio=8:年輕代中,2個(gè)Survivor區(qū)與1個(gè)Eden區(qū)比例=2:8,Survivor占新生代內(nèi)存比例為1/5
-XX:MaxHeapFreeRatio=70:堆內(nèi)存使用率大于70時(shí)擴(kuò)張堆內(nèi)存,xms=xmx時(shí)該參數(shù)無(wú)效
-XX:MinHeapFreeRatio=40:堆內(nèi)存使用率小于40時(shí)縮減堆內(nèi)存,xms=xmx時(shí)該參數(shù)無(wú)效。
備注:堆大小=年輕代(Young Generation)+年老代(Old Generation),Young Generation=Eden Space + 2 Survivor Space。
非堆設(shè)置--持久代設(shè)置
-XX:PermSize=56M:初始分配的持久代容量,默認(rèn)為物理內(nèi)存的1/64(從1.8起,該參數(shù)已作廢,可使用-XX:MetaspaceSize替代)
-XX:MaxPermSize=56M:持久代的最大容量,默認(rèn)為物理內(nèi)存的1/4,設(shè)置值過(guò)小會(huì)導(dǎo)致java.lang.OutOfMemoryError: PermGen space錯(cuò)誤。(從1.8起,該參數(shù)已作廢,可使用-XX:MaxMetaspaceSize替代)
非堆設(shè)置--虛擬機(jī)棧
-Xss128k:虛擬機(jī)棧大小為128k
GC相關(guān)
-Xnoclassgc:關(guān)閉JVM垃圾回收功能
-XX:+UseG1GC:使用G1垃圾回收器
-XX:+DisableExplicitGC:靜止Java程序中的FULL GC,如System.gc()
-XX:PrintGCDetails:GC時(shí)打印詳細(xì)信息
-XX:PrintGC:GC時(shí)打印信息
-XX:PrintHeapAtGC:GC時(shí)打印堆前后信息
其他
-XX:+HeapDumpOnOutOfMemoryError:內(nèi)存溢出時(shí)Dump出當(dāng)前堆內(nèi)存快照
參考:
https://blog.csdn.net/zc19921215/article/details/83029952
https://www.cnblogs.com/airnew/p/11756450.html
https://blog.csdn.net/u010588262/article/details/81365547
http://www.manongjc.com/article/91948.html
https://www.cnblogs.com/cecWork/p/13061071.html
https://www.cnblogs.com/huigelaile/p/11188231.html