java9 gc log参数迁移

本文详细介绍了Java9引入的统一JVM及GC日志框架,解释了如何使用-Xlog参数进行GC日志配置,包括tag、level、output等选项,并提供了从旧版GC参数到新版Xlog配置的迁移指南。

原文链接:https://segmentfault.com/a/1190000013475524


本文主要研究一下java9 gc log参数的迁移。

统一JVM及GC的Logging

java9引进了一个统一的日志框架,对gc log的输出进行了统一的配置。

相关JEP(JDK Enhancement Proposal)

Xlog语法

-Xlog[:option]
    option         :=  [<what>][:[<output>][:[<decorators>][:<output-options>]]]
                       'help'
                       'disable'
    what           :=  <selector>[,...]
    selector       :=  <tag-set>[*][=<level>]
    tag-set        :=  <tag>[+...]
                       'all'
    tag            :=  name of tag
    level          :=  trace
                       debug
                       info
                       warning
                       error
    output         :=  'stderr'
                       'stdout'
                       [file=]<filename>
    decorators     :=  <decorator>[,...]
                       'none'
    decorator      :=  time
                       uptime
                       timemillis
                       uptimemillis
                       timenanos
                       uptimenanos
                       pid
                       tid
                       level
                       tags
    output-options :=  <output_option>[,...]
    output-option  :=  filecount=<file count>
                       filesize=<file size in kb>
                       parameter=value

what

主要是配置tag及level

tag

其中all代表所有的tag,其他的如下:

add,age,alloc,annotation,aot,arguments,attach,barrier,biasedlocking,blocks,bot,breakpoint,bytecode,census,class,classhisto,cleanup,compaction,comparator,constraints,constantpool,coops,cpu,cset,data,defaultmethods,dump,ergo,event,exceptions,exit,fingerprint,freelist,gc,hashtables,heap,humongous,ihop,iklass,init,itables,jfr,jni,jvmti,liveness,load,loader,logging,mark,marking,metadata,metaspace,method,mmu,modules,monitorinflation,monitormismatch,nmethod,normalize,objecttagging,obsolete,oopmap,os,pagesize,parser,patchpath,phases,plab,preorder,promotion,protectiondomain,purge,redefine,ref,refine,region,remset,resolve,safepoint,scavenge,scrub,setting,stackmap,stacktrace,stackwalk,start,startuptime,state,stats,stringdedup,stringtable,subclass,survivor,sweep,system,task,threadtime,timer,tlab,unload,update,verification,verify,vmoperation,vtables,workgang

level

主要分off,trace,debug,info,warning,error

output

- stdout(`Sends output to stdout`)
- stderr(`Sends output to stderr`)
- file=filename(`Sends output to text file(s)`)
有如上三种,其中指定file的话,可以使用%p变量表示当前jvm的pid,用%t表示jvm的启动时间戳。比如
-Xlog:gc:demoapp-gc-%p-%t.log

输出的文件名如下:

demoapp-gc-1678-2018-03-01_21-44-18.log

decorators

  • time -- Current time and date in ISO-8601 format
  • uptime -- Time since the start of the JVM in seconds and milliseconds (e.g., 6.567s)
  • timemillis -- The same value as generated by System.currentTimeMillis()
  • uptimemillis -- Milliseconds since the JVM started
  • timenanos -- The same value as generated by System.nanoTime()
  • uptimenanos -- Nanoseconds since the JVM started
  • pid -- The process identifier
  • tid -- The thread identifier
  • level -- The level associated with the log message
  • tags -- The tag-set associated with the log message
不指定的话,默认是uptime, level, and tags这三个。比如
[3.080s][info][gc,cpu        ] GC(5) User=0.03s Sys=0.00s Real=0.01s

实例

-Xlog:gc=trace:file=gctrace.txt:uptimemillis,pid:filecount=5,filesize=1024
tag为gc,levle为trace,rotate文件数为5,每个文件1M,文件名为gctrace.txt,decrotators为uptimemillis和pid

输出实例

[1110ms][1867] GC(2) Pause Remark 17M->17M(256M) 2.024ms
[1110ms][1867] GC(2) Finalize Live Data 0.000ms
[1110ms][1867] GC(2) Pause Cleanup 17M->17M(256M) 0.177ms
[1112ms][1867] GC(2) Concurrent Cycle 7.470ms
[2951ms][1867] GC(3) Pause Initial Mark (Metadata GC Threshold) 149M->30M(256M) 27.175ms
[2951ms][1867] GC(4) Concurrent Cycle
[2972ms][1867] GC(4) Pause Remark 32M->32M(256M) 5.132ms
[2974ms][1867] GC(4) Finalize Live Data 0.000ms
[2974ms][1867] GC(4) Pause Cleanup 32M->32M(256M) 0.214ms
[2976ms][1867] GC(4) Concurrent Cycle 25.422ms

迁移

旧版GC相关参数迁移

Legacy Garbage Collection (GC) FlagXlog ConfigurationComment
G1PrintHeapRegions-Xlog:gc+region=traceNot Applicable
GCLogFileSizeNo configuration availableLog rotation is handled by the framework.
NumberOfGCLogFilesNot ApplicableLog rotation is handled by the framework.
PrintAdaptiveSizePolicy-Xlog:ergo*=levelUse a level of debug for most of the information, or a level of trace for all of what was logged for PrintAdaptiveSizePolicy.
PrintGC-Xlog:gcNot Applicable
PrintGCApplicationConcurrentTime-Xlog:safepointNote that PrintGCApplicationConcurrentTime and PrintGCApplicationStoppedTime are logged on the same tag and aren’t separated in the new logging.
PrintGCApplicationStoppedTime-Xlog:safepointNote that PrintGCApplicationConcurrentTime and PrintGCApplicationStoppedTime are logged on the same tag and not separated in the new logging.
PrintGCCauseNot ApplicableGC cause is now always logged.
PrintGCDateStampsNot ApplicableDate stamps are logged by the framework.
PrintGCDetails-Xlog:gc*Not Applicable
PrintGCIDNot ApplicableGC ID is now always logged.
PrintGCTaskTimeStamps-Xlog:task*=debugNot Applicable
PrintGCTimeStampsNot ApplicableTime stamps are logged by the framework.
PrintHeapAtGC-Xlog:gc+heap=traceNot Applicable
PrintReferenceGC-Xlog:ref*=debugNote that in the old logging, PrintReferenceGC had an effect only if PrintGCDetails was also enabled.
PrintStringDeduplicationStatistics-Xlog:stringdedup*=debugNot Applicable
PrintTenuringDistribution-Xlog:age*=levelUse a level of debug for the most relevant information, or a level of trace for all of what was logged for PrintTenuringDistribution.
UseGCLogFileRotationNot ApplicableWhat was logged for PrintTenuringDistribution.

旧版运行时参数迁移

Legacy Runtime FlagXlog ConfigurationComment
TraceExceptions-Xlog:exceptions=infoNot Applicable
TraceClassLoading-Xlog:class+load=levelUse level=info for regular information, or level=debug for additional information. In Unified Logging syntax, -verbose:class equals -Xlog:class+load=info,class+unload=info.
TraceClassLoadingPreorder-Xlog:class+preorder=debugNot Applicable
TraceClassUnloading-Xlog:class+unload=levelUse level=info for regular information, or level=trace for additional information. In Unified Logging syntax, -verbose:class equals -Xlog:class+load=info,class+unload=info.
VerboseVerification-Xlog:verification=infoNot Applicable
TraceClassPaths-Xlog:class+path=infoNot Applicable
TraceClassResolution-Xlog:class+resolve=debugNot Applicable
TraceClassInitialization-Xlog:class+init=infoNot Applicable
TraceLoaderConstraints-Xlog:class+loader+constraints=infoNot Applicable
TraceClassLoaderData-Xlog:class+loader+data=levelUse level=debug for regular information or level=trace for additional information.
TraceSafepointCleanupTime-Xlog:safepoint+cleanup=infoNot Applicable
TraceSafepoint-Xlog:safepoint=debugNot Applicable
TraceMonitorInflation-Xlog:monitorinflation=debugNot Applicable
TraceBiasedLocking-Xlog:biasedlocking=levelUse level=info for regular information, or level=trace for additional information.
TraceRedefineClasses-Xlog:redefine+class*=levellevel=info, =debug, and =trace provide increasing amounts of information.

小结

java9把gc log的配置统一到了Xlog中,可以按照官方给出的新旧参数映射表进行迁移。

doc

<think>嗯,用户想了解JVMGC日志的参数配置,这涉及JVM调优的关键部分。结合三篇引用内容,特别是新版JVM的变化,可以整理出清晰的配置方案。引用1提到G1GC配置示例和日志输出路径,引用2详细对比了Java9前后日志参数的差异,引用3补充了元数据区等重要参数。这说明需要区分Java版本讲解,用户可能正在迁移旧项目或优化生产环境配置。注意到用户明确要求"详解",准备从日志框架演变讲起。考虑划分模块说明:基础参数(输出开关/路径)、格式控制(时间戳/级别)、高级选项(堆/元空间)和版本适配策略。对于Java9+要重点解释-Xlog指令的灵活性,比如使用装饰器添加附加信息的方式可能正是用户需要的。示例部分将展示两种典型场景:开发环境调试用详细GC日志(带时间戳和堆信息),生产环境轻量日志(仅记录关键事件)。考虑引用2里的可视化工具GcViewer也应该包含在后续问题中,用户分析日志时可能需要。用户应该是后端开发或运维,三篇引用都偏向实战,推测更关注具体参数效果而非理论。准备避免过多GC算法原理解释,直接关联参数设置与实际输出效果的关系。比如-XX:+PrintAdaptiveSizePolicy对于排查G1动态调整特别有用。考虑到存在版本兼容问题,结尾决定添加版本适配说明。这样用户无论使用Java8还是11+都能直接套用模板。后续问题设置了参数优化和异常排查方向,这和调优的实际工作流程一致:先配日志再看数据最后解决问题。</think> 以下是基于Java不同版本的JVM GC日志参数详解,结合核心配置项和实际示例说明: --- ### **一、GC日志核心参数配置** #### **1. 日志输出基础控制** | 参数 | 作用 | 适用版本 | |------|------|----------| | `-Xlog:gc*` | 开启所有GC事件日志记录 | Java 9+ | | `-XX:+PrintGC` | 输出简要GC信息 | Java 8- | | `-XX:+PrintGCDetails` | 输出详细GC堆内存变化 | Java 8- | | `-Xlog:gc:gc.log` | 将日志输出到gc.log文件 | Java 9+ | | `-XX:+PrintGCDateStamps` | 添加时间戳(需配合`PrintGC`) | Java 8- | #### **2. 日志格式增强** | 参数 | 作用 | |------|------| | `-Xlog:gc*,time:gc_time.log` | 添加精确时间戳(Java 9+)<sup>[^2]</sup> | | `-Xlog:gc*:file=gc.log:tags,uptime` | 日志添加线程/时间信息(Java 9+) | | `-XX:+PrintTenuringDistribution` | 打印对象年龄分布(用于分析晋升行为) | #### **3. 堆与GC算法参数** ```bash -XX:+UseG1GC # 启用G1收集器 -XX:MaxGCPauseMillis=100 # 目标最大停顿时间(ms) -Xms512m -Xmx512m # 堆初始/最大内存 -XX:MetaspaceSize=128m # 元空间初始大小(Java 8+取代永久代) -XX:MaxMetaspaceSize=256m # 元空间上限<sup>[^3]</sup> ``` --- ### **二、不同版本配置示例** #### **▶ Java 8及之前** ```bash -XX:+UseG1GC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:gc.log -XX:MaxMetaspaceSize=256m ``` #### **▶ Java 9及之后(推荐)** ```bash -Xlog:gc*,gc+heap*,gc+age*:file=gc.log:time,level,tags -XX:+UseG1GC -XX:MaxGCPauseMillis=150 -Xms512m -Xmx512m ``` --- ### **三、日志关键内容解析** **示例日志片段(G1 GC)**: ```log [2023-10-01T12:00:00.123+0800][info][gc,heap] GC(42) Eden regions: 50->0(50) [info][gc,heap] GC(42) Survivor regions: 5->5(10) [info][gc,heap] GC(42) Old regions: 30->35 [info][gc,heap] GC(42) Humongous regions: 1->1 [info][gc ] GC(42) Pause Young (Normal) 25ms ``` - **Eden/Survivor**:年轻代区域变化 - **Old regions**:老年代占用增长 - **Pause Young**:年轻代GC停顿时间 --- ### **四、可视化分析工具** 1. **GcViewer**:开源工具,解析GC日志生成图表 ![GC暂停时间直方图示例](https://gcviewer.org/img/screen1.jpg) (可分析暂停时间分布、内存趋势等) 2. **通用日志分析方法**: - 高频Full GC → 检查老年代/元空间设置 - 长暂停时间 → 调整`MaxGCPauseMillis`或堆大小 - Survivor区频繁清零 → 对象过早晋升 --- ### **五、版本适配注意** - **Java 9+** 使用`-Xlog`统一日志框架,废弃`PrintGCDetails`等旧参数<sup>[^2]</sup> - **Java 17+** 默认G1收集器,建议显式配置`-XX:+UseG1GC`保持兼容性 --- ### **相关问题扩展** 1. 如何分析GC日志中的 **"Allocation Failure"** 报错? 2. G1与ZGC的日志参数配置有何差异? 3. 生产环境如何平衡 **详细日志记录** 与 **磁盘IO开销**? 4. 怎样通过GC日志判断 **内存泄漏**? > 参考资料: > [^1]: Java高阶私房菜:GC日志实战配置 > [^2]: jvm-47: GC日志获取与GcViewer分析 > [^3]: JVM元空间参数详解
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值