【Python】分析JVM的GC日志
在项目启动命令中增加JVM参数
nohup /usr/local/jdk1.8.0_361/bin/java -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -Xloggc:/usr/local/logs/gc.log -jar /opt/test.jar --spring.profiles.active=prod > /dev/null 2>&1 &
说明:
-XX:+PrintGCDetails 打印GC的详细信息
-XX:+PrintGCTimeStamps 打印进程启动到现在经历的时间 启用配置:
-XX:+PrintGCDateStamps 打印GC当时的时间 启用配置:
-XX:+PrintGCApplicationStoppedTime 打印GC时,应用停顿时间 启用配置:
-XX:+PrintHeapAtGC 每次GC前后打印堆信息启用配置:
-Xloggc:/usr/local/logs/gc.log GC日志输出路径 启用配置:
gc.log样例
2025-01-06T17:48:27.813+0800: 1.280: [Full GC (Metadata GC Threshold) [PSYoungGen: 5093K->0K(132096K)] [ParOldGen: 2137K->6947K(51712K)] 7231K->6947K(183808K), [Metaspace: 20369K->20369K(1067008K)], 0.0365654 secs] [Times: user=0.08 sys=0.00, real=0.04 secs]
2025-01-06T17:48:28.082+0800: 1.548: [GC (Allocation Failure) [PSYoungGen: 126976K->1728K(203776K)] 133923K->8683K(255488K), 0.0045147 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
python处理日志
import re
from datetime import datetime
def parse_gc_log(log_file):
gc_logs = []
with open(log_file, 'r') as file:
for line in file:
match = re.match(r'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}\+\d{4}): (\d+\.\d+): \[(GC|Full GC) \((.*?)\) \[(.*?): (\d+)K->(\d+)K\((\d+)K\)\] (\d+)K->(\d+)K\((\d+)K\),(.*?) (\d+\.\d+) secs\](.*?)\[Times: user=(\d+\.\d+) sys=(\d+\.\d+), real=(\d+\.\d+) secs\]', line)
if match:
gc_start_time = datetime.strptime(match.group(1), '%Y-%m-%dT%H:%M:%S.%f%z')
gc = match.group(3)
gc_type = match.group(4)
gc_pause_time = match.group(13)
gc_logs.append({
'gc_start_time': gc_start_time,
'gc': gc,
'gc_type': gc_type,
'gc_pause_time': gc_pause_time
})
return gc_logs
def main():
log_file = "D:\\temp\\gc.log"
gc_logs = parse_gc_log(log_file)
# for log in gc_logs:
# print(f"GC Start Time: {log['gc_start_time']},GC : {log['gc']}, GC Type: {log['gc_type']}, GC Pause Time: {log['gc_pause_time']} secs")
with open('D:\\temp\\gc.txt', 'w') as file:
for log in gc_logs:
file.write(f"GC Start Time: {log['gc_start_time']},GC : {log['gc']}, GC Type: {log['gc_type']}, GC Pause Time: {log['gc_pause_time']} secs\n")
if __name__ == "__main__":
main()
输出样例
GC Start Time: 2025-01-06 17:48:27.813000+08:00,GC : Full GC, GC Type: Metadata GC Threshold, GC Pause Time: 0.0365654 secs
GC Start Time: 2025-01-06 17:48:28.082000+08:00,GC : GC, GC Type: Allocation Failure, GC Pause Time: 0.0045147 secs