1. JVM 参数及意义
参数及示例 | 意义 | 备注 |
---|---|---|
-server | 服务器模式 | |
-Xms6G | 初始堆大小 | |
-Xmx6G | 最大堆大小 | |
-Xmn4G | 新生代大小 | |
-XX:NewRatio=1 | 老年代与年轻代比例,老年代 :年轻代 = 1 | 默认2 |
-XX:SurvivorRatio=8 | 年轻代中Eden与Survivor比例, Eden : Survivor = 8 | survivor有两个 8:1:1 |
-XX:MaxTenuringThreshold=0 | 设置对象进入老年代之前的垃圾回收次数,为0则直接进入老年代 | |
-XX:+UseParNewGC | 新生代使用ParNew | |
-XX:ParallelGCThreads=3 | 设置ParNew并发回收线程数 | |
-XX:+UseConcMarkSweepGC | 老年代使用cms | |
-XX:CMSInitiatingOccupancyFraction=70 | 当老年代使用率达到此值之后,并行收集器便开始进行垃圾收集 | |
-XX:+UseCMSInitiatingOccupancyOnly | 设置CMS收集器只使用设定的回收阈值(上面指定的70%),如果不指定,JVM仅在第一次使用设定值,后续则自动调整 | |
-XX:+CMSParallelRemarkEnabled | 采用并行标记方式降低停顿。 | 默认开启 |
-XX:+UseCMSCompactAtFullCollection | FullGC后压缩 | 默认开启 |
-XX:CMSFullGCsBeforeCompaction=5 | 多少次FullGC后做压缩 | |
-XX:+CMSScavengeBeforeRemark | 在CMS GC前启动一次ygc,目的在于减少old gen对ygc gen的引用,降低remark时的开销 | |
-XX:+CMSClassUnloadingEnabled | 设置JVM中CMS收集器对元空间(永久代)进行回收 | |
-XX:MetaspaceSize=256M | 元空间初始大小 | 默认21M |
-XX:MaxMetaspaceSize=512M | 元空间最大大小 | |
-XX:+PrintGCDetails | 打印gc日志 | |
-XX:+PrintGCDateStamps | 打印时间戳 | |
-Xloggc:/export/Logs/gc.log | 指定gc log的位置 | |
-XX:+HeapDumpOnOutOfMemoryError | oom时生成dump文件 | |
-XX:HeapDumpPath=/export/Logs | dump文件位置 | |
-XX:ErrorFile=./hs_err_pid.log | jvm异常时记录日志 | |
-XX:NativeMemoryTracking=detail | 堆外内存 | |
-XX:MaxDirectMemorySize=2G | 最大堆外内存大小 |
2. 举个gc日志的例子
Java HotSpot™ 64-Bit Server VM (25.371-b11) for linux-amd64 JRE (1.8.0_371-b11), built on Mar 17 2023 04:01:49 by “java_re” with gcc 7.3.0
Memory: 4k page, physical 67108864k(21180736k free), swap 0k(0k free)
CommandLine flags: -XX:InitialHeapSize=8589934592 -XX:MaxHeapSize=8589934592 -XX:NativeMemoryTracking=detail -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+U
seCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC
1.768: [GC (Metadata GC Threshold) [PSYoungGen: 1048857K->9036K(2446848K)] 1048857K->9124K(8039424K), 0.0104430 secs] [Times: user=0.05 sys=0.00, real=0.01 secs]
1.779: [Full GC (Metadata GC Threshold) [PSYoungGen: 9036K->0K(2446848K)] [ParOldGen: 88K->8843K(5592576K)] 9124K->8843K(8039424K), [Metaspace: 20544K->20542K(1067008K)],
0.0358162 secs] [Times: user=0.09 sys=0.01, real=0.03 secs]
3.269: [GC (Metadata GC Threshold) [PSYoungGen: 1113443K->14582K(2446848K)] 1122287K->23433K(8039424K), 0.0122841 secs] [Times: user=0.05 sys=0.01, real=0.01 secs]
3.282: [Full GC (Metadata GC Threshold) [PSYoungGen: 14582K->0K(2446848K)] [ParOldGen: 8851K->21135K(5592576K)] 23433K->21135K(8039424K), [Metaspace: 34394K->34394K(108134
4K)], 0.0758200 secs] [Times: user=0.33 sys=0.01, real=0.08 secs]
6.896: [GC (Allocation Failure) [PSYoungGen: 2097664K->20649K(2446848K)] 2118799K->41793K(8039424K), 0.0215613 secs] [Times: user=0.13 sys=0.00, real=0.03 secs]
9.041: [GC (Metadata GC Threshold) [PSYoungGen: 2046420K->35616K(2446848K)] 2067563K->56775K(8039424K), 0.0200844 secs] [Times: user=0.11 sys=0.01, real=0.02 secs]
9.061: [Full GC (Metadata GC Threshold) [PSYoungGen: 35616K->0K(2446848K)] [ParOldGen: 21159K->51994K(5592576K)] 56775K->51994K(8039424K), [Metaspace: 56847K->56847K(11018
24K)], 0.1523636 secs] [Times: user=0.84 sys=0.01, real=0.15 secs]
12.313: [GC (Allocation Failure) [PSYoungGen: 2097664K->13040K(2446848K)] 2149658K->65043K(8039424K), 0.0147675 secs] [Times: user=0.04 sys=0.00, real=0.02 secs]
15.574: [GC (Metadata GC Threshold) [PSYoungGen: 1547808K->36850K(2743808K)] 1599811K->89837K(8336384K), 0.0245040 secs] [Times: user=0.11 sys=0.04, real=0.03 secs]
15.599: [Full GC (Metadata GC Threshold) [PSYoungGen: 36850K->0K(2743808K)] [ParOldGen: 52987K->86798K(5592576K)] 89837K->86798K(8336384K), [Metaspace: 95119K->95119K(1136
640K)], 0.2141273 secs] [Times: user=1.12 sys=0.01, real=0.21 secs]
18.971: [GC (Allocation Failure) [PSYoungGen: 2706944K->44528K(2751488K)] 2793742K->710393K(8344064K), 0.4209614 secs] [Times: user=2.49 sys=0.79, real=0.42 secs]
110.726: [GC (Allocation Failure) [PSYoungGen: 2751431K->44529K(1920000K)] 3417297K->1104620K(7512576K), 0.3131204 secs] [Times: user=1.86 sys=0.50, real=0.32 secs]
128.818: [GC (Allocation Failure) [PSYoungGen: 1919985K->223187K(2335744K)] 2980076K->1283286K(7928320K), 0.0230622 secs] [Times: user=0.12 sys=0.00, real=0.02 secs]
131.276: [GC (Allocation Failure) [PSYoungGen: 2098643K->216969K(2346496K)] 3158742K->1277077K(7939072K), 0.0287557 secs] [Times: user=0.15 sys=0.00, real=0.03 secs]
288.667: [GC (Allocation Failure) [PSYoungGen: 2106761K->216805K(2339328K)] 3166869K->1276921K(7931904K), 0.0272056 secs] [Times: user=0.12 sys=0.00, real=0.03 secs]
332.130: [GC (Allocation Failure) [PSYoungGen: 2106597K->231208K(2357760K)] 3166713K->1291331K(7950336K), 0.0256538 secs] [Times: user=0.13 sys=0.00, real=0.02 secs]
359.758: [GC (Allocation Failure) [PSYoungGen: 2147112K->254558K(2354176K)] 3207235K->1314689K(7946752K), 0.0471532 secs] [Times: user=0.23 sys=0.01, real=0.04 secs]
370.955: [GC (Allocation Failure) [PSYoungGen: 2170462K->226842K(2363392K)] 3230593K->1286982K(7955968K), 0.0373914 secs] [Times: user=0.18 sys=0.01, real=0.04 secs]
374.897: [GC (Allocation Failure) [PSYoungGen: 2148378K->228636K(2354176K)] 3208518K->1288783K(7946752K), 0.0330555 secs] [Times: user=0.14 sys=0.00, real=0.04 secs]
415.567: [GC (Allocation Failure) [PSYoungGen: 2150172K->229118K(2380800K)] 3210319K->1289273K(7973376K), 0.0301935 secs] [Times: user=0.14 sys=0.00, real=0.03 secs]
465.779: [GC (Allocation Failure) [PSYoungGen: 2185982K->268541K(2372096K)] 3246137K->1328704K(7964672K), 0.0516660 secs] [Times: user=0.19 sys=0.00, real=0.05 secs]
3. -verbose:gc 与 -XX:PrintGC的区别
-verbose:gc
稳定版本
参见:http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html
-XX:+PrintGC
非稳定版本,可能在未通知的情况下删除,在下面官方文档中是-XX:-PrintGC。
因为被标记为manageable,所以可以通过如下三种方式修改:
1、com.sun.management.HotSpotDiagnosticMXBean API
2、JConsole
3、jinfo -flag
参见:http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html