4種GC日志解讀和分析
目標
- 并行GC演示和分析
- 串行GC演示和分析
- CMSGC演示和分析
- G1GC演示和分析
- GC 如何選擇
如下是模擬記憶體溢位的Java代碼
案例代碼存在生產垃圾物件,當垃圾物件超過最大堆記憶體時,就造成記憶體溢位
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;
/*
演示GC日志生成與解讀
*/
public class GCLogAnalysis {
private static Random random = new Random();
public static void main(String[] args) {
// 當前毫秒時間戳
long startMillis = System.currentTimeMillis();
// 持續運行毫秒數; 可根據需要進行修改
long timeoutMillis = TimeUnit.SECONDS.toMillis(1);
// 結束時間戳
long endMillis = startMillis + timeoutMillis;
LongAdder counter = new LongAdder();
System.out.println("正在執行...");
// 快取一部分物件; 進入老年代
int cacheSize = 2000;
Object[] cachedGarbage = new Object[cacheSize];
// 在此時間范圍內,持續回圈
while (System.currentTimeMillis() < endMillis) {
// 生成垃圾物件
Object garbage = generateGarbage(100*1024);
counter.increment();
int randomIndex = random.nextInt(2 * cacheSize);
if (randomIndex < cacheSize) {
cachedGarbage[randomIndex] = garbage;
}
}
System.out.println("執行結束!共生成物件次數:" + counter.longValue());
}
// 生成物件
private static Object generateGarbage(int max) {
int randomSize = random.nextInt(max);
int type = randomSize % 4;
Object result = null;
switch (type) {
case 0:
result = new int[randomSize];
break;
case 1:
result = new byte[randomSize];
break;
case 2:
result = new double[randomSize];
break;
default:
StringBuilder builder = new StringBuilder();
String randomString = "randomString-Anything";
while (builder.length() < randomSize) {
builder.append(randomString);
builder.append(max);
builder.append(randomSize);
}
result = builder.toString();
break;
}
return result;
}
}
演示GC日志生成與解讀
- 當時在win下編譯的,需要加引數-encoding UTF-8,linux不需要 (win默認使用GBDK字符,如果檔案是UTF-8不能編譯)
D:\F盤\JavaCourseCodes\01jvm>javac -encoding UTF-8 GCLogAnalysis.java
- 執行GCLogAnalysis檔案,列印的GC日志生產到當前目錄下demo.log
D:\F盤\JavaCourseCodes\01jvm>java -Xloggc:gc.demo.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps
-XX:MaxHeapSize=4244185088,如果沒有設定最大堆記憶體,默認是服務器的最大記憶體的1/4大小 ,默認采用UseParallelGC 并行GC
Java HotSpot(TM) 64-Bit Server VM (25.201-b09) for windows-amd64 JRE (1.8.0_201-b09), built on Dec 15 2018 18:36:39 by "java_re" with MS VC++ 10.0 (VS2010)
Memory: 4k page, physical 16578848k(2260340k free), swap 30350536k(4206764k free)
CommandLine flags: -XX:InitialHeapSize=265261568 -XX:MaxHeapSize=4244185088 -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
//-XX:MaxHeapSize=4244185088,如果沒有設定最大堆記憶體,默認是服務器的最大記憶體的1/4大小 ,默認采用UseParallelGC 并行GC
2021-02-23T07:34:15.501+0800: 0.274: [GC (Allocation Failure) [PSYoungGen: 65024K->10747K(75776K)] 65024K->23007K(249344K), 0.0072693 secs] [Times: user=0.03 sys=0.08, real=0.01 secs]
//65024K->10747K(75776K)] 65024K->23007K(249344K), 0.0072693 secs] :young區從65M >10M,Old區從65M > 23M ,old區減到23M,說明此時部分物件從young提升到old區
//Times: user=0.03 用戶執行緒耗時 sys=0.08 系統耗時, real=0.01 secs STW暫停時間
2021-02-23T07:34:15.531+0800: 0.304: [GC (Allocation Failure) [PSYoungGen: 75204K->10744K(140800K)] 87463K->42977K(314368K), 0.0097131 secs] [Times: user=0.03 sys=0.05, real=0.01 secs]
2021-02-23T07:34:15.610+0800: 0.382: [GC (Allocation Failure) [PSYoungGen: 140564K->10747K(140800K)] 172797K->88342K(314368K), 0.0153475 secs] [Times: user=0.03 sys=0.08, real=0.02 secs]
2021-02-23T07:34:15.664+0800: 0.436: [GC (Allocation Failure) [PSYoungGen: 140795K->10749K(270848K)] 218390K->127729K(444416K), 0.0157620 secs] [Times: user=0.02 sys=0.08, real=0.02 secs]
2021-02-23T07:34:15.810+0800: 0.583: [GC (Allocation Failure) [PSYoungGen: 270845K->10747K(270848K)] 387825K->204045K(465408K), 0.0291165 secs] [Times: user=0.03 sys=0.19, real=0.03 secs]
2021-02-23T07:34:15.839+0800: 0.612: [Full GC (Ergonomics) [PSYoungGen: 10747K->0K(270848K)] [ParOldGen: 193297K->170244K(330240K)] 204045K->170244K(601088K), [Metaspace: 2628K->2628K(1056768K)], 0.0301415 secs] [Times: user=0.16 sys=0.00, real=0.03 secs]
//經過五次youngGC(每次GC耗時遞增),old區越來越大,此時進行fullGC一次,, 0.0301415 secs對比youngGC,fullGC耗時非常多
//fungGC時,young區直接減到0,old區從193M > 170M
2021-02-23T07:34:15.941+0800: 0.713: [GC (Allocation Failure) [PSYoungGen: 260096K->78120K(546816K)] 430340K->248364K(877056K), 0.0249608 secs] [Times: user=0.02 sys=0.06, real=0.03 secs]
2021-02-23T07:34:16.189+0800: 0.961: [GC (Allocation Failure) [PSYoungGen: 546600K->101880K(588800K)] 716844K->353984K(919040K), 0.0510303 secs] [Times: user=0.11 sys=0.22, real=0.05 secs]
2021-02-23T07:34:16.240+0800: 1.013: [Full GC (Ergonomics) [PSYoungGen: 101880K->0K(588800K)] [ParOldGen: 252103K->266559K(460800K)] 353984K->266559K(1049600K), [Metaspace: 2628K->2628K(1056768K)], 0.0374054 secs] [Times: user=0.20 sys=0.00, real=0.04 secs]
2021-02-23T07:34:16.398+0800: 1.171: [GC (Allocation Failure) [PSYoungGen: 486345K->142934K(982016K)] 752905K->409493K(1442816K), 0.0431602 secs] [Times: user=0.05 sys=0.22, real=0.04 secs]
Heap
PSYoungGen total 982016K, used 176414K [0x000000076bb00000, 0x00000007b5680000, 0x00000007c0000000)
eden space 821760K, 4% used [0x000000076bb00000,0x000000076dbb2160,0x000000079dd80000)
from space 160256K, 89% used [0x00000007a8e00000,0x00000007b1995908,0x00000007b2a80000)
to space 180736K, 0% used [0x000000079dd80000,0x000000079dd80000,0x00000007a8e00000)
ParOldGen total 460800K, used 266559K [0x00000006c3000000, 0x00000006df200000, 0x000000076bb00000)
object space 460800K, 57% used [0x00000006c3000000,0x00000006d344fda0,0x00000006df200000)
Metaspace used 2634K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 291K, capacity 386K, committed 512K, reserved 1048576K
執行結束!共生成物件次數:8885
- 設定初始最小和最大對大小一樣時,
共生成物件次數:12035,性能對沒有設定初始化最小堆時提升39%,解讀:Xms設定與-Xmx相同,以避免每次垃圾回收完成后JVM重新分配記憶體
D:\F盤\JavaCourseCodes\01jvm>java -Xms4g -Xmx4g -XX:+PrintGCDetails -XX:+PrintGCDateStamps GCLogAnalysis
正在執行...
2021-02-24T06:48:52.516+0800: [GC (Allocation Failure) [PSYoungGen: 1048576K->174589K(1223168K)] 1048576K->235252K(4019712K), 0.0441413 secs] [Times: user=0.02 sys=0.20, real=0.04 secs]
2021-02-24T06:48:52.718+0800: [GC (Allocation Failure) [PSYoungGen: 1223165K->174591K(1223168K)] 1283828K->357504K(4019712K), 0.1024186 secs] [Times: user=0.23 sys=0.52, real=0.10 secs]
2021-02-24T06:48:52.978+0800: [GC (Allocation Failure) [PSYoungGen: 1223167K->174577K(1223168K)] 1406080K->473105K(4019712K), 0.4661695 secs] [Times: user=0.27 sys=2.50, real=0.47 secs]
執行結束!共生成物件次數:12035
Heap
PSYoungGen total 1223168K, used 216650K [0x000000076ab00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 1048576K, 4% used [0x000000076ab00000,0x000000076d416238,0x00000007aab00000)
from space 174592K, 99% used [0x00000007aab00000,0x00000007b557c750,0x00000007b5580000)
to space 174592K, 0% used [0x00000007b5580000,0x00000007b5580000,0x00000007c0000000)
ParOldGen total 2796544K, used 298527K [0x00000006c0000000, 0x000000076ab00000, 0x000000076ab00000)
object space 2796544K, 10% used [0x00000006c0000000,0x00000006d2387f40,0x000000076ab00000)
Metaspace used 2634K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 291K, capacity 386K, committed 512K, reserved 1048576K
D:\F盤\JavaCourseCodes\01jvm>
- 圖解說明并行GC的記憶體變化

每次youngGC,young區空間減少很多,Eden和S0全部清空,部分物件復制到S1區,old區越來越大,然后進行fullGC,下一次youngGC會把Eden和S1清空 ,
延伸問題:年輕代和老年代的垃圾回收都會觸發STW 事件,在年輕代使用標記-復制(mark-copy)演算法,在老年代使用標記-清除-整理(mark-sweepcompact)演算法,-XX:ParallelGCThreads=N 來指定GC 執行緒數, 其默認值為CPU 核心數,
fullGC,會把Eden ,S0,S1區全部清空
默認yungGC次數15次,超過15次還存活的物件采用移動方式晉升到老年代區,老年代能減少些但不多
串行GC演示( -XX:+UseSerialGC)
- 當前最大堆記憶體512M
D:\F盤\JavaCourseCodes\01jvm>java -XX:+UseSerialGC -Xms512m -Xmx512m -XX:+PrintGCDetails -XX:+PrintGCDateStamps GCLogAnalysis
正在執行...
2021-02-23T22:35:13.453+0800: [GC (Allocation Failure) 2021-02-23T22:35:13.453+0800: [DefNew: 139776K->17472K(157248K), 0.0324775 secs] 139776K->48832K(506816K), 0.0330806 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
2021-02-23T22:35:13.521+0800: [GC (Allocation Failure) 2021-02-23T22:35:13.521+0800: [DefNew: 157127K->17471K(157248K), 0.0427665 secs] 188488K->98605K(506816K), 0.0434445 secs] [Times: user=0.03 sys=0.00, real=0.04 secs]
2021-02-23T22:35:13.607+0800: [GC (Allocation Failure) 2021-02-23T22:35:13.608+0800: [DefNew: 157247K->17471K(157248K), 0.0323721 secs] 238381K->140525K(506816K), 0.0331904 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
2021-02-23T22:35:13.682+0800: [GC (Allocation Failure) 2021-02-23T22:35:13.682+0800: [DefNew: 157247K->17471K(157248K), 0.0337171 secs] 280301K->184841K(506816K), 0.0344639 secs] [Times: user=0.02 sys=0.01, real=0.03 secs]
2021-02-23T22:35:13.757+0800: [GC (Allocation Failure) 2021-02-23T22:35:13.758+0800: [DefNew: 156573K->17472K(157248K), 0.0318421 secs] 323942K->226784K(506816K), 0.0327063 secs] [Times: user=0.01 sys=0.02, real=0.03 secs]
2021-02-23T22:35:13.831+0800: [GC (Allocation Failure) 2021-02-23T22:35:13.832+0800: [DefNew: 157248K->17471K(157248K), 0.0357931 secs] 366560K->273563K(506816K), 0.0364656 secs] [Times: user=0.02 sys=0.02, real=0.04 secs]
2021-02-23T22:35:13.909+0800: [GC (Allocation Failure) 2021-02-23T22:35:13.910+0800: [DefNew: 157098K->17471K(157248K), 0.0321501 secs] 413190K->312630K(506816K), 0.0330849 secs] [Times: user=0.02 sys=0.02, real=0.03 secs]
2021-02-23T22:35:13.981+0800: [GC (Allocation Failure) 2021-02-23T22:35:13.981+0800: [DefNew: 157247K->17470K(157248K), 0.0342253 secs] 452406K->354246K(506816K), 0.0349803 secs] [Times: user=0.02 sys=0.02, real=0.04 secs]
2021-02-23T22:35:14.053+0800: [GC (Allocation Failure) 2021-02-23T22:35:14.054+0800: [DefNew: 157246K->157246K(157248K), 0.0002210 secs]2021-02-23T22:35:14.054+0800: [Tenured: 336776K->273961K(349568K), 0.0635833 secs] 494022K->273961K(506816K), [Metaspace: 2628K->2628K(1056768K)], 0.0645362 secs] [Times: user=0.06 sys=0.00, real=0.07 secs]
2021-02-23T22:35:14.155+0800: [GC (Allocation Failure) 2021-02-23T22:35:14.155+0800: [DefNew: 139776K->17471K(157248K), 0.0137345 secs] 413737K->324486K(506816K), 0.0144490 secs] [Times: user=0.01 sys=0.00, real=0.02 secs]
2021-02-23T22:35:14.207+0800: [GC (Allocation Failure) 2021-02-23T22:35:14.207+0800: [DefNew: 157247K->157247K(157248K), 0.0002225 secs]2021-02-23T22:35:14.207+0800: [Tenured: 307014K->301297K(349568K), 0.0715632 secs] 464262K->301297K(506816K), [Metaspace: 2628K->2628K(1056768K)], 0.0724522 secs] [Times: user=0.06 sys=0.00, real=0.07 secs]
執行結束!共生成物件次數:6288
Heap
def new generation total 157248K, used 126441K [0x00000000e0000000, 0x00000000eaaa0000, 0x00000000eaaa0000)
eden space 139776K, 90% used [0x00000000e0000000, 0x00000000e7b7a608, 0x00000000e8880000)
from space 17472K, 0% used [0x00000000e9990000, 0x00000000e9990000, 0x00000000eaaa0000)
to space 17472K, 0% used [0x00000000e8880000, 0x00000000e8880000, 0x00000000e9990000)
tenured generation total 349568K, used 301297K [0x00000000eaaa0000, 0x0000000100000000, 0x0000000100000000)
the space 349568K, 86% used [0x00000000eaaa0000, 0x00000000fd0dc620, 0x00000000fd0dc800, 0x0000000100000000)
Metaspace used 2634K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 291K, capacity 386K, committed 512K, reserved 1048576K
D:\F盤\JavaCourseCodes\01jvm>
- 當前最大堆記憶體128M時,出現記憶體溢位例外了
fullGC對垃圾回收幾乎沒有作用,垃圾一直占的記憶體空間
D:\F盤\JavaCourseCodes\01jvm>java -XX:+UseSerialGC -Xms128m -Xmx128m -XX:+PrintGCDetails -XX:+PrintGCDateStamps GCLogAnalysis
正在執行...
2021-02-23T22:42:07.153+0800: [GC (Allocation Failure) 2021-02-23T22:42:07.154+0800: [DefNew: 34915K->4351K(39296K), 0.0105513 secs] 34915K->12378K(126720K), 0.0115875 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2021-02-23T22:42:07.182+0800: [GC (Allocation Failure) 2021-02-23T22:42:07.182+0800: [DefNew: 39197K->4349K(39296K), 0.0123236 secs] 47223K->22965K(126720K), 0.0138073 secs] [Times: user=0.00 sys=0.02, real=0.01 secs]
2021-02-23T22:42:07.213+0800: [GC (Allocation Failure) 2021-02-23T22:42:07.214+0800: [DefNew: 38618K->4343K(39296K), 0.0091259 secs] 57234K->33897K(126720K), 0.0100570 secs] [Times: user=0.01 sys=0.02, real=0.01 secs]
2021-02-23T22:42:07.237+0800: [GC (Allocation Failure) 2021-02-23T22:42:07.238+0800: [DefNew: 39146K->4351K(39296K), 0.0104840 secs] 68701K->45751K(126720K), 0.0111278 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-23T22:42:07.265+0800: [GC (Allocation Failure) 2021-02-23T22:42:07.266+0800: [DefNew: 38929K->4338K(39296K), 0.0086908 secs] 80329K->55306K(126720K), 0.0096184 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2021-02-23T22:42:07.288+0800: [GC (Allocation Failure) 2021-02-23T22:42:07.288+0800: [DefNew: 39269K->4348K(39296K), 0.0137101 secs] 90238K->68836K(126720K), 0.0144757 secs] [Times: user=0.00 sys=0.02, real=0.01 secs]
2021-02-23T22:42:07.313+0800: [GC (Allocation Failure) 2021-02-23T22:42:07.313+0800: [DefNew: 39292K->4349K(39296K), 0.0142091 secs] 103780K->83540K(126720K), 0.0150465 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
2021-02-23T22:42:07.344+0800: [GC (Allocation Failure) 2021-02-23T22:42:07.350+0800: [DefNew: 39008K->39008K(39296K), 0.0011520 secs]2021-02-23T22:42:07.352+0800: [Tenured: 79190K->87419K(87424K), 0.0237885 secs] 118199K->90743K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0361671 secs] [Times: user=0.02 sys=0.00, real=0.04 secs]
2021-02-23T22:42:07.394+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.396+0800: [Tenured: 87419K->87397K(87424K), 0.0191946 secs] 126695K->99685K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0212197 secs] [Times: user=0.01 sys=0.00, real=0.02 secs]
2021-02-23T22:42:07.429+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.429+0800: [Tenured: 87397K->87232K(87424K), 0.0221254 secs] 126683K->106532K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0232227 secs] [Times: user=0.01 sys=0.00, real=0.02 secs]
2021-02-23T22:42:07.462+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.463+0800: [Tenured: 87304K->87268K(87424K), 0.0306981 secs] 126540K->108960K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0328040 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
2021-02-23T22:42:07.504+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.506+0800: [Tenured: 87268K->87268K(87424K), 0.0079315 secs] 126362K->113405K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0106992 secs] [Times: user=0.00 sys=0.02, real=0.01 secs]
2021-02-23T22:42:07.520+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.529+0800: [Tenured: 87268K->87268K(87424K), 0.0050639 secs] 126526K->116875K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0144243 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
2021-02-23T22:42:07.541+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.550+0800: [Tenured: 87268K->87398K(87424K), 0.0116193 secs] 126410K->118705K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0215769 secs] [Times: user=0.00 sys=0.00, real=0.02 secs]
2021-02-23T22:42:07.569+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.574+0800: [Tenured: 87398K->87249K(87424K), 0.0315818 secs] 126427K->116175K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0369570 secs] [Times: user=0.03 sys=0.00, real=0.04 secs]
2021-02-23T22:42:07.613+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.616+0800: [Tenured: 87249K->87249K(87424K), 0.0091864 secs] 126101K->119359K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0126861 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-23T22:42:07.634+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.636+0800: [Tenured: 87249K->87249K(87424K), 0.0047839 secs] 126005K->121203K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0077264 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-23T22:42:07.651+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.651+0800: [Tenured: 87249K->87249K(87424K), 0.0049760 secs] 126470K->123275K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0061458 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-23T22:42:07.669+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.680+0800: [Tenured: 87357K->87423K(87424K), 0.0342295 secs] 126618K->121943K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0459635 secs] [Times: user=0.03 sys=0.00, real=0.05 secs]
2021-02-23T22:42:07.717+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.717+0800: [Tenured: 87423K->87423K(87424K), 0.0109540 secs] 126119K->123382K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0122313 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2021-02-23T22:42:07.737+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.742+0800: [Tenured: 87423K->87423K(87424K), 0.0046415 secs] 126694K->124383K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0101329 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-23T22:42:07.753+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.768+0800: [Tenured: 87423K->87423K(87424K), 0.0114733 secs] 126437K->124440K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0268546 secs] [Times: user=0.00 sys=0.00, real=0.03 secs]
2021-02-23T22:42:07.786+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.794+0800: [Tenured: 87423K->87362K(87424K), 0.0319215 secs] 126709K->124372K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0403032 secs] [Times: user=0.03 sys=0.00, real=0.04 secs]
2021-02-23T22:42:07.831+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.832+0800: [Tenured: 87362K->87362K(87424K), 0.0037068 secs] 126024K->125099K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0073521 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-23T22:42:07.840+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.842+0800: [Tenured: 87362K->87362K(87424K), 0.0046366 secs] 125758K->125758K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0095181 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-23T22:42:07.851+0800: [Full GC (Allocation Failure) 2021-02-23T22:42:07.866+0800: [Tenured: 87362K->87279K(87424K), 0.0246226 secs] 125758K->125675K(126720K), [Metaspace: 2628K->2628K(1056768K)], 0.0399895 secs] [Times: user=0.01 sys=0.00, real=0.04 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at GCLogAnalysis.generateGarbage(GCLogAnalysis.java:48)
at GCLogAnalysis.main(GCLogAnalysis.java:25)
Heap
def new generation total 39296K, used 38845K [0x00000000f8000000, 0x00000000faaa0000, 0x00000000faaa0000)
eden space 34944K, 100% used [0x00000000f8000000, 0x00000000fa220000, 0x00000000fa220000)
from space 4352K, 89% used [0x00000000fa660000, 0x00000000faa2f408, 0x00000000faaa0000)
to space 4352K, 0% used [0x00000000fa220000, 0x00000000fa220000, 0x00000000fa660000)
tenured generation total 87424K, used 87279K [0x00000000faaa0000, 0x0000000100000000, 0x0000000100000000)
the space 87424K, 99% used [0x00000000faaa0000, 0x00000000fffdbe90, 0x00000000fffdc000, 0x0000000100000000)
Metaspace used 2658K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 294K, capacity 386K, committed 512K, reserved 1048576K
D:\F盤\JavaCourseCodes\01jvm>
- 當前最大堆記憶體2048M時,
此時GC次數只有兩次了,但是GC耗時很久,對比設定最大堆記憶體512M時產生的物件減少了20%,也就是說性能反而較少,Xmx不是越大越好
D:\F盤\JavaCourseCodes\01jvm>java -XX:+UseSerialGC -Xms2048m -Xmx2048m -XX:+PrintGCDetails -XX:+PrintGCDateStamps GCLogAnalysis
正在執行...
2021-02-23T22:47:56.035+0800: [GC (Allocation Failure) 2021-02-23T22:47:56.035+0800: [DefNew: 559232K->69888K(629120K), 0.1193025 secs] 559232K->157347K(2027264K), 0.1198591 secs] [Times: user=0.06 sys=0.05, real=0.12 secs]
2021-02-23T22:47:56.304+0800: [GC (Allocation Failure) 2021-02-23T22:47:56.304+0800: [DefNew: 629120K->69888K(629120K), 0.1559310 secs] 716579K->289236K(2027264K), 0.1568711 secs] [Times: user=0.09 sys=0.06, real=0.16 secs]
執行結束!共生成物件次數:4917
Heap
def new generation total 629120K, used 291713K [0x0000000080000000, 0x00000000aaaa0000, 0x00000000aaaa0000)
eden space 559232K, 39% used [0x0000000080000000, 0x000000008d8a07a8, 0x00000000a2220000)
from space 69888K, 100% used [0x00000000a2220000, 0x00000000a6660000, 0x00000000a6660000)
to space 69888K, 0% used [0x00000000a6660000, 0x00000000a6660000, 0x00000000aaaa0000)
tenured generation total 1398144K, used 219348K [0x00000000aaaa0000, 0x0000000100000000, 0x0000000100000000)
the space 1398144K, 15% used [0x00000000aaaa0000, 0x00000000b80d53e8, 0x00000000b80d5400, 0x0000000100000000)
Metaspace used 2634K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 291K, capacity 386K, committed 512K, reserved 1048576K
D:\F盤\JavaCourseCodes\01jvm>
CMSGC演示( -XX:+UseConcMarkSweepGC)
- 當前最大堆記憶體512M
當發生5五次youngGC后,就進行CMSGC,
[GC (CMS Initial Mark) [1 CMS-initial-mark: 207528K(349568K)] 225479K(506816K), 0.0009352 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Initial Mark(初始標記):當前步驟需要JVM暫停STW,標記的根物件GC root
[CMS-concurrent-mark-start] Concurrent Mark(并發標記)
[CMS-concurrent-mark: 0.002/0.002 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Concurrent Preclean(并發預清理)
[CMS-concurrent-preclean-start] Final Remark(最終標記)
[CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Concurrent Sweep(并發清除)
[CMS-concurrent-abortable-preclean-start] Concurrent Reset(并發重置)
一共6個步驟,5個步驟是并發執行,CMS 使用的并發執行緒數等于CPU 核心數的1/4CMS GC 的設計目標是避免在老年代垃圾收集時出現長時間的卡頓
D:\F盤\JavaCourseCodes\01jvm>java -XX:+UseConcMarkSweepGC -Xms512m -Xmx512m -XX:+PrintGCDetails -XX:+PrintGCDateStamps GCLogAnalysis
正在執行...
2021-02-24T07:02:07.839+0800: [GC (Allocation Failure) 2021-02-24T07:02:07.840+0800: [ParNew: 139776K->17468K(157248K), 0.0079185 secs] 139776K->44649K(506816K), 0.0085489 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-24T07:02:07.879+0800: [GC (Allocation Failure) 2021-02-24T07:02:07.879+0800: [ParNew: 157199K->17459K(157248K), 0.0130003 secs] 184380K->97930K(506816K), 0.0133819 secs] [Times: user=0.03 sys=0.09, real=0.01 secs]
2021-02-24T07:02:07.923+0800: [GC (Allocation Failure) 2021-02-24T07:02:07.923+0800: [ParNew: 157235K->17472K(157248K), 0.0219433 secs] 237706K->138548K(506816K), 0.0223873 secs] [Times: user=0.08 sys=0.02, real=0.02 secs]
2021-02-24T07:02:07.972+0800: [GC (Allocation Failure) 2021-02-24T07:02:07.972+0800: [ParNew: 157248K->17472K(157248K), 0.0225044 secs] 278324K->181476K(506816K), 0.0231603 secs] [Times: user=0.09 sys=0.02, real=0.02 secs]
2021-02-24T07:02:08.023+0800: [GC (Allocation Failure) 2021-02-24T07:02:08.024+0800: [ParNew: 156802K->17471K(157248K), 0.0235213 secs] 320806K->225000K(506816K), 0.0239944 secs] [Times: user=0.17 sys=0.03, real=0.02 secs]
2021-02-24T07:02:08.049+0800: [GC (CMS Initial Mark) [1 CMS-initial-mark: 207528K(349568K)] 225479K(506816K), 0.0009352 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.050+0800: [CMS-concurrent-mark-start]
2021-02-24T07:02:08.052+0800: [CMS-concurrent-mark: 0.002/0.002 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.053+0800: [CMS-concurrent-preclean-start]
2021-02-24T07:02:08.055+0800: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.055+0800: [CMS-concurrent-abortable-preclean-start]
2021-02-24T07:02:08.082+0800: [GC (Allocation Failure) 2021-02-24T07:02:08.083+0800: [ParNew: 157247K->17471K(157248K), 0.0207909 secs] 364776K->262029K(506816K), 0.0213737 secs] [Times: user=0.09 sys=0.00, real=0.02 secs]
2021-02-24T07:02:08.130+0800: [GC (Allocation Failure) 2021-02-24T07:02:08.131+0800: [ParNew: 157247K->17470K(157248K), 0.0221870 secs] 401805K->300663K(506816K), 0.0227283 secs] [Times: user=0.17 sys=0.02, real=0.02 secs]
2021-02-24T07:02:08.176+0800: [GC (Allocation Failure) 2021-02-24T07:02:08.176+0800: [ParNew: 157246K->17471K(157248K), 0.0268265 secs] 440439K->348831K(506816K), 0.0273241 secs] [Times: user=0.17 sys=0.01, real=0.03 secs]
2021-02-24T07:02:08.205+0800: [CMS-concurrent-abortable-preclean: 0.006/0.146 secs] [Times: user=0.51 sys=0.03, real=0.15 secs]
2021-02-24T07:02:08.206+0800: [GC (CMS Final Remark) [YG occupancy: 25957 K (157248 K)]2021-02-24T07:02:08.207+0800: [Rescan (parallel) , 0.0004543 secs]2021-02-24T07:02:08.207+0800: [weak refs processing, 0.0000867 secs]2021-02-24T07:02:08.207+0800: [class unloading, 0.0008495 secs]2021-02-24T07:02:08.208+0800: [scrub symbol table, 0.0004637 secs]2021-02-24T07:02:08.209+0800: [scrub string table, 0.0002037 secs][1 CMS-remark: 331359K(349568K)] 357316K(506816K), 0.0035973 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.210+0800: [CMS-concurrent-sweep-start]
2021-02-24T07:02:08.211+0800: [CMS-concurrent-sweep: 0.001/0.001 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.214+0800: [CMS-concurrent-reset-start]
2021-02-24T07:02:08.222+0800: [CMS-concurrent-reset: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-24T07:02:08.234+0800: [GC (Allocation Failure) 2021-02-24T07:02:08.234+0800: [ParNew: 157247K->17471K(157248K), 0.0133071 secs] 444418K->351340K(506816K), 0.0137356 secs] [Times: user=0.09 sys=0.00, real=0.01 secs]
2021-02-24T07:02:08.249+0800: [GC (CMS Initial Mark) [1 CMS-initial-mark: 333869K(349568K)] 351485K(506816K), 0.0008960 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.250+0800: [CMS-concurrent-mark-start]
2021-02-24T07:02:08.253+0800: [CMS-concurrent-mark: 0.002/0.002 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-24T07:02:08.260+0800: [CMS-concurrent-preclean-start]
2021-02-24T07:02:08.262+0800: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.263+0800: [CMS-concurrent-abortable-preclean-start]
2021-02-24T07:02:08.263+0800: [CMS-concurrent-abortable-preclean: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.265+0800: [GC (CMS Final Remark) [YG occupancy: 79605 K (157248 K)]2021-02-24T07:02:08.265+0800: [Rescan (parallel) , 0.0004118 secs]2021-02-24T07:02:08.266+0800: [weak refs processing, 0.0001727 secs]2021-02-24T07:02:08.266+0800: [class unloading, 0.0011286 secs]2021-02-24T07:02:08.267+0800: [scrub symbol table, 0.0004355 secs]2021-02-24T07:02:08.268+0800: [scrub string table, 0.0001893 secs][1 CMS-remark: 333869K(349568K)] 413475K(506816K), 0.0038621 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-24T07:02:08.275+0800: [CMS-concurrent-sweep-start]
2021-02-24T07:02:08.278+0800: [CMS-concurrent-sweep: 0.001/0.001 secs] [Times: user=0.03 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.278+0800: [CMS-concurrent-reset-start]
2021-02-24T07:02:08.280+0800: [CMS-concurrent-reset: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.292+0800: [GC (Allocation Failure) 2021-02-24T07:02:08.292+0800: [ParNew: 157112K->17470K(157248K), 0.0115372 secs] 387069K->288883K(506816K), 0.0123590 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-24T07:02:08.306+0800: [GC (CMS Initial Mark) [1 CMS-initial-mark: 271412K(349568K)] 289062K(506816K), 0.0008636 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.307+0800: [CMS-concurrent-mark-start]
2021-02-24T07:02:08.309+0800: [CMS-concurrent-mark: 0.002/0.002 secs] [Times: user=0.05 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.310+0800: [CMS-concurrent-preclean-start]
2021-02-24T07:02:08.311+0800: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.312+0800: [CMS-concurrent-abortable-preclean-start]
2021-02-24T07:02:08.334+0800: [GC (Allocation Failure) 2021-02-24T07:02:08.335+0800: [ParNew: 156988K->17471K(157248K), 0.0125048 secs] 428400K->327382K(506816K), 0.0129594 secs] [Times: user=0.11 sys=0.00, real=0.01 secs]
2021-02-24T07:02:08.375+0800: [GC (Allocation Failure) 2021-02-24T07:02:08.375+0800: [ParNew: 157199K->157199K(157248K), 0.0001566 secs]2021-02-24T07:02:08.376+0800: [CMS2021-02-24T07:02:08.376+0800: [CMS-concurrent-abortable-preclean: 0.003/0.060 secs] [Times: user=0.16 sys=0.00, real=0.06 secs]
(concurrent mode failure): 309910K->289393K(349568K), 0.0562658 secs] 467109K->289393K(506816K), [Metaspace: 2628K->2628K(1056768K)], 0.0569689 secs] [Times: user=0.05 sys=0.00, real=0.06 secs]
2021-02-24T07:02:08.459+0800: [GC (Allocation Failure) 2021-02-24T07:02:08.459+0800: [ParNew: 139776K->17472K(157248K), 0.0088130 secs] 429169K->336093K(506816K), 0.0093098 secs] [Times: user=0.09 sys=0.00, real=0.01 secs]
2021-02-24T07:02:08.470+0800: [GC (CMS Initial Mark) [1 CMS-initial-mark: 318621K(349568K)] 336237K(506816K), 0.0009292 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.471+0800: [CMS-concurrent-mark-start]
2021-02-24T07:02:08.475+0800: [CMS-concurrent-mark: 0.002/0.002 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.475+0800: [CMS-concurrent-preclean-start]
2021-02-24T07:02:08.477+0800: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.479+0800: [CMS-concurrent-abortable-preclean-start]
2021-02-24T07:02:08.486+0800: [CMS-concurrent-abortable-preclean: 0.000/0.000 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2021-02-24T07:02:08.488+0800: [GC (CMS Final Remark) [YG occupancy: 88636 K (157248 K)]2021-02-24T07:02:08.491+0800: [Rescan (parallel) , 0.0004964 secs]2021-02-24T07:02:08.492+0800: [weak refs processing, 0.0001096 secs]2021-02-24T07:02:08.492+0800: [class unloading, 0.0059392 secs]2021-02-24T07:02:08.498+0800: [scrub symbol table, 0.0006367 secs]2021-02-24T07:02:08.499+0800: [scrub string table, 0.0002811 secs][1 CMS-remark: 318621K(349568K)] 407257K(506816K), 0.0135319 secs] [Times: user=0.00 sys=0.00, real=0.02 secs]
2021-02-24T07:02:08.503+0800: [CMS-concurrent-sweep-start]
2021-02-24T07:02:08.516+0800: [GC (Allocation Failure) 2021-02-24T07:02:08.516+0800: [ParNew: 157248K->17471K(157248K), 0.0143191 secs] 443039K->350400K(506816K), 0.0145845 secs] [Times: user=0.08 sys=0.00, real=0.01 secs]
2021-02-24T07:02:08.530+0800: [CMS-concurrent-sweep: 0.001/0.016 secs] [Times: user=0.09 sys=0.00, real=0.04 secs]
2021-02-24T07:02:08.540+0800: [CMS-concurrent-reset-start]
2021-02-24T07:02:08.547+0800: [CMS-concurrent-reset: 0.000/0.000 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2021-02-24T07:02:08.560+0800: [GC (Allocation Failure) 2021-02-24T07:02:08.561+0800: [ParNew: 157247K->157247K(157248K), 0.0001587 secs]2021-02-24T07:02:08.561+0800: [CMS: 328412K->312126K(349568K), 0.0522486 secs] 485660K->312126K(506816K), [Metaspace: 2628K->2628K(1056768K)], 0.0537724 secs] [Times: user=0.05 sys=0.00, real=0.05 secs]
2021-02-24T07:02:08.614+0800: [GC (CMS Initial Mark) [1 CMS-initial-mark: 312126K(349568K)] 312630K(506816K), 0.0007227 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.615+0800: [CMS-concurrent-mark-start]
2021-02-24T07:02:08.618+0800: [CMS-concurrent-mark: 0.002/0.002 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.618+0800: [CMS-concurrent-preclean-start]
2021-02-24T07:02:08.620+0800: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.03 sys=0.00, real=0.00 secs]
2021-02-24T07:02:08.620+0800: [CMS-concurrent-abortable-preclean-start]
2021-02-24T07:02:08.625+0800: [CMS-concurrent-abortable-preclean: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-24T07:02:08.631+0800: [GC (CMS Final Remark) [YG occupancy: 88449 K (157248 K)]2021-02-24T07:02:08.632+0800: [Rescan (parallel) , 0.0004337 secs]2021-02-24T07:02:08.632+0800: [weak refs processing, 0.0014522 secs]2021-02-24T07:02:08.634+0800: [class unloading, 0.0100824 secs]2021-02-24T07:02:08.644+0800: [scrub symbol table, 0.0004037 secs]2021-02-24T07:02:08.645+0800: [scrub string table, 0.0002123 secs][1 CMS-remark: 312126K(349568K)] 400576K(506816K), 0.0142744 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-02-24T07:02:08.646+0800: [CMS-concurrent-sweep-start]
2021-02-24T07:02:08.656+0800: [GC (Allocation Failure) 2021-02-24T07:02:08.663+0800: [ParNew: 139566K->139566K(157248K), 0.0001837 secs]2021-02-24T07:02:08.663+0800: [CMS2021-02-24T07:02:08.663+0800: [CMS-concurrent-sweep: 0.001/0.008 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
(concurrent mode failure): 311478K->323317K(349568K), 0.0660418 secs] 451044K->323317K(506816K), [Metaspace: 2628K->2628K(1056768K)], 0.0729739 secs] [Times: user=0.05 sys=0.00, real=0.07 secs]
執行結束!共生成物件次數:8831
Heap
par new generation total 157248K, used 111872K [0x00000000e0000000, 0x00000000eaaa0000, 0x00000000eaaa0000)
eden space 139776K, 80% used [0x00000000e0000000, 0x00000000e6d400b8, 0x00000000e8880000)
from space 17472K, 0% used [0x00000000e9990000, 0x00000000e9990000, 0x00000000eaaa0000)
to space 17472K, 0% used [0x00000000e8880000, 0x00000000e8880000, 0x00000000e9990000)
concurrent mark-sweep generation total 349568K, used 323317K [0x00000000eaaa0000, 0x0000000100000000, 0x0000000100000000)
Metaspace used 2634K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 291K, capacity 386K, committed 512K, reserved 1048576K
D:\F盤\JavaCourseCodes\01jvm>
- 圖解GC變化

G1GC演示(-XX:+UseG1GC)
- 當前最大堆記憶體512M
當發生5五次youngGC后,就進行CMSGC,
階段1: Initial Mark(初始標記)
階段2: Root Region Scan(Root區掃描)
階段3:Evacuation Pause (mixed)(轉移暫停: 混合模式)
階段4: Remark(再次標記)
階段5: Cleanup(清理)
D:\F盤\JavaCourseCodes\01jvm>java -XX:+UseG1GC -Xms512m -Xmx512m -XX:+PrintGC -XX:+PrintGCDateStamps GCLogAnalysis
正在執行...
2021-02-24T07:28:03.140+0800: [GC pause (G1 Evacuation Pause) (young) 32M->11M(512M), 0.0028508 secs] //Evacuation Pause: young(純年輕代模式轉移暫停)
2021-02-24T07:28:03.153+0800: [GC pause (G1 Evacuation Pause) (young) 39M->20M(512M), 0.0027907 secs]
2021-02-24T07:28:03.178+0800: [GC pause (G1 Evacuation Pause) (young) 67M->37M(512M), 0.0042861 secs]
2021-02-24T07:28:03.208+0800: [GC pause (G1 Evacuation Pause) (young) 102M->60M(512M), 0.0052239 secs]
2021-02-24T07:28:03.276+0800: [GC pause (G1 Evacuation Pause) (young) 214M->108M(512M), 0.0072020 secs]
2021-02-24T07:28:03.316+0800: [GC pause (G1 Evacuation Pause) (young) 242M->149M(512M), 0.0077214 secs]
2021-02-24T07:28:03.383+0800: [GC pause (G1 Humongous Allocation) (young) (initial-mark) 342M->202M(512M), 0.0342600 secs] //Concurrent Marking(并發標記)
2021-02-24T07:28:03.417+0800: [GC concurrent-root-region-scan-start]
2021-02-24T07:28:03.419+0800: [GC concurrent-root-region-scan-end, 0.0012973 secs]
2021-02-24T07:28:03.419+0800: [GC concurrent-mark-start]
2021-02-24T07:28:03.422+0800: [GC concurrent-mark-end, 0.0024423 secs]
2021-02-24T07:28:03.422+0800: [GC remark, 0.0017725 secs]
2021-02-24T07:28:03.424+0800: [GC cleanup 228M->228M(512M), 0.0012586 secs]
//階段1: Initial Mark(初始標記)
//階段2: Root Region Scan(Root區掃描)
//階段3:Evacuation Pause (mixed)(轉移暫停: 混合模式)
//階段4: Remark(再次標記)
//階段5: Cleanup(清理)
2021-02-24T07:28:03.471+0800: [GC pause (G1 Evacuation Pause) (young)-- 406M->307M(512M), 0.0083992 secs]
2021-02-24T07:28:03.480+0800: [GC pause (G1 Evacuation Pause) (mixed) 310M->299M(512M), 0.0048689 secs] //Evacuation Pause (mixed)(轉移暫停: 混合模式)
2021-02-24T07:28:03.487+0800: [GC pause (G1 Humongous Allocation) (young) (initial-mark) 303M->300M(512M), 0.0011917 secs]
2021-02-24T07:28:03.489+0800: [GC concurrent-root-region-scan-start]
2021-02-24T07:28:03.497+0800: [GC concurrent-root-region-scan-end, 0.0085547 secs]
2021-02-24T07:28:03.504+0800: [GC concurrent-mark-start]
2021-02-24T07:28:03.507+0800: [GC concurrent-mark-end, 0.0026832 secs]
2021-02-24T07:28:03.508+0800: [GC remark, 0.0113277 secs]
2021-02-24T07:28:03.520+0800: [GC cleanup 383M->383M(512M), 0.0010538 secs]
2021-02-24T07:28:03.529+0800: [GC pause (G1 Evacuation Pause) (young) 412M->332M(512M), 0.0084896 secs]
2021-02-24T07:28:03.541+0800: [GC pause (G1 Evacuation Pause) (mixed) 349M->291M(512M), 0.0047697 secs]
2021-02-24T07:28:03.552+0800: [GC pause (G1 Evacuation Pause) (mixed) 316M->278M(512M), 0.0044825 secs]
2021-02-24T07:28:03.558+0800: [GC pause (G1 Humongous Allocation) (young) (initial-mark) 283M->279M(512M), 0.0016376 secs]
2021-02-24T07:28:03.560+0800: [GC concurrent-root-region-scan-start]
2021-02-24T07:28:03.569+0800: [GC concurrent-root-region-scan-end, 0.0091800 secs]
- 圖解GC變化

- 延伸知識
通過劃分多個記憶體區域做增量整理和回收,進一步降低延遲
G1 的全稱是Garbage-First,意為垃圾優先,哪一塊的垃圾最多就優先清理它
G1 GC 最主要的設計目標是:將STW 停頓的時間和分布,變成可預期且可配置的
堆不再分成年輕代和老年代,而是劃分為多個(通常是2048 個)可以存放物件的小塊堆區域(smaller heap regions),每個小塊,可能一會被定義成Eden 區,一會被指定為Survivor區或者Old 區,在邏輯上,所有的Eden 區和Survivor區合起來就是年輕代,所有的Old 區拼在一起那就是老年
Java8默認并行GC,Java9, Java11 默認G1GC
GC 如何選擇
- 如果系統考慮吞吐優先,CPU 資源都用來最大程度處理業務,用Parallel GC;
- 如果系統考慮低延遲有限,每次GC 時間盡量短,用CMS GC;
- 如果系統記憶體堆較大,同時希望整體來看平均GC 時間可控,使用G1 GC,.對于記憶體大小的考量:
1. 一般4G 以上,算是比較大,用G1 的性價比較高,
2. 一般超過8G,比如16G-64G 記憶體,非常推薦使用G1 GC
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/263431.html
標籤:java
下一篇:遞回+IO(位元組流)
