GC日志解读
打印GC日志
例如查看执行类GCLogAnalysis.class时的GC情况
package com.homework.practice.jvm02;
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;
}
}
//加载并执行字节码文件GCLogAnalysis 同时在控制台打印GC日志
java -XX:+PrintGCDetails GCLogAnalysis
D:\MyJavaProject\online-study\Practice\Practice\src\main\java>java -XX:+PrintGCDetails com.homework.practice.jvm02.GCLogAnalysis
正在执行...
[GC (Allocation Failure) [PSYoungGen: 64642K->10743K(75776K)] 64642K->26433K(249344K), 0.0050514 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
/*内容解读:
1、(Allocation Failure)表示触发GC的原因是内存分配失败
2、PSYoungGen: GC前使用:64642K->GC后使用:10743K(年轻代总大小:75776K)
3、64642K->26433K(249344K)表示堆内存总大小GC前->GC后的变化以及(总的堆内存大小),0.0050514 secs GC暂停(STW)的时间
4、2和3 GC前相同说明此时堆内存只有young区有对象,GC后young区回收的内存大于总堆内存回收的大小,因为有部分对象"晋升"导致的
5、Times: user=0.00 sys=0.00, real=0.01 secs 表示cpu使用时间
*/
[GC (Allocation Failure) [PSYoungGen: 75767K->10749K(140800K)] 91457K->46064K(314368K), 0.0054614 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 140797K->10750K(140800K)] 176112K->91506K(314368K), 0.0108372 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 140798K->10739K(270848K)] 221554K->129247K(444416K), 0.0093983 secs] [Times: user=0.00 sys=0.20, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 270835K->10751K(270848K)] 389343K->211889K(472064K), 0.0180200 secs] [Times: user=0.06 sys=0.14, real=0.02 secs]
[Full GC (Ergonomics) [PSYoungGen: 10751K->0K(270848K)] [ParOldGen: 201137K->174261K(336384K)] 211889K->174261K(607232K), [Metaspace: 2697K->2697K(1056768K)], 0.0233039 secs] [Times: user=0.41 sys=0.00, real=0.02 secs]
[GC (Allocation Failure) [PSYoungGen: 260096K->76667K(540160K)] 434357K->250929K(876544K), 0.0143926 secs] [Times: user=0.03 sys=0.17, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 540027K->101375K(589312K)] 714289K->348965K(925696K), 0.0290516 secs] [Times: user=0.09 sys=0.31, real=0.03 secs]
[Full GC (Ergonomics) [PSYoungGen: 101375K->0K(589312K)] [ParOldGen: 247589K->263886K(459776K)] 348965K->263886K(1049088K), [Metaspace: 2697K->2697K(1056768K)], 0.0348643 secs] [Times: user=0.34 sys=0.06, real=0.04 secs]
[GC (Allocation Failure) [PSYoungGen: 487936K->136430K(963072K)] 751822K->400317K(1422848K), 0.0263687 secs] [Times: user=0.06 sys=0.34, real=0.03 secs]
[GC (Allocation Failure) [PSYoungGen: 944366K->173049K(980992K)] 1208253K->526848K(1440768K), 0.0467042 secs] [Times: user=0.11 sys=0.50, real=0.05 secs]
[GC (Allocation Failure) [PSYoungGen: 980985K->237054K(1030144K)] 1334784K->639115K(1489920K), 0.0501990 secs] [Times: user=0.11 sys=0.70, real=0.05 secs]
[Full GC (Ergonomics) [PSYoungGen: 237054K->0K(1030144K)] [ParOldGen: 402061K->360696K(584192K)] 639115K->360696K(1614336K), [Metaspace: 2697K->2697K(1056768K)], 0.0476122 secs] [Times: user=0.59 sys=0.02, real=0.05 secs]
[GC (Allocation Failure) [PSYoungGen: 793059K->203431K(996864K)] 1153756K->564127K(1581056K), 0.0239517 secs] [Times: user=0.20 sys=0.00, real=0.02 secs]
[GC (Allocation Failure) [PSYoungGen: 996519K->200211K(1052160K)] 1357215K->653768K(1636352K), 0.0358869 secs] [Times: user=0.27 sys=0.14, real=0.04 secs]
执行结束!共生成对象次数:19251
Heap
PSYoungGen total 1052160K, used 230718K [0x000000076bf80000, 0x00000007c0000000, 0x00000007c0000000)
eden space 745984K, 4% used [0x000000076bf80000,0x000000076dd4af18,0x0000000799800000)
from space 306176K, 65% used [0x00000007ad500000,0x00000007b9884c18,0x00000007c0000000)
to space 315392K, 0% used [0x0000000799800000,0x0000000799800000,0x00000007acc00000)
ParOldGen total 584192K, used 453557K [0x00000006c3e00000, 0x00000006e7880000, 0x000000076bf80000)
object space 584192K, 77% used [0x00000006c3e00000,0x00000006df8ed638,0x00000006e7880000)
Metaspace used 2704K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 289K, capacity 386K, committed 512K, reserved 1048576K
//加载并执行字节码文件GCLogAnalysis 同时在日志文件gc.demo.log和控制台打印GC日志,并且会多输出GC发生的时间
java -Xloggc:gc.demo.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps GCLogAnalysis
//加载并执行字节码文件GCLogAnalysis 同时在日志文件gc.demo.log和控制台打印GC日志概览,并且会多输出GC发生的时间
java -Xloggc:gc.demo.log -XX:+PrintGC -XX:+PrintGCDateStamps GCLogAnalysis
GC日志解读工具
将得到的GC日志复制到对应的工具中,就能得到具体且详细的分析
GCEasy
需要进入网址gceasy.io