Try to Avoid -XX UseGCLogFileRotation

Try to Avoid -XX:+UseGCLogFileRotation

Source:http://link.zhihu.com/?target=https%3A//dzone.com/articles/try-to-avoid-xxusegclogfilerotation

Developers take advantage of the JVM argument -XX:+UseGCLogFileRotation to rotate GC log files.

开发人员利用JVM参数-XX:+UseGCLogFileRotation来递换GC日志文件。

-XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/home/GCEASY/gc.log -
XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=20M"

As shown above, the JVM will rotate the GC log file whenever its size reaches 20MB. It will generate up to five files, with extensions gc.log.0,  gc.log.1gc.log.2gc.log.3, and gc.log.4.

如上所示的配置会产生5个日志文件,并且每个日志文件有20M。

Losing Old GC Logs

Suppose you configured  -XX:NumberOfGCLogFiles=5, then over a period of time, five GC log files will be created:

假设你配置了-XX:NumberOfGCLogFiles=5,那么在一段时期内,将创建五个GC日志文件。

  • gc.log.0 ← _oldest GC Log content_
  • gc.log.1
  • gc.log.2
  • gc.log.3
  • gc.log.4 ← _latest GC Log content_

The most recent GC log contents will be written to gc.log.4 and old GC log contents will be present in gc.log.0.

最新的GC日志内容将被写入gc.log.4,旧的GC日志内容将出现在gc.log.0

When the application starts to generate more GC logs than the configured  -XX:NumberOfGCLogFiles, in this case, five, then old GC log contents in gc.log.0 will be deleted. New GC events will be written to  gc.log.0. It means that you will end up not having all the generated GC logs. You will lose the visibility of all events.

当应用程序配置的-XX:NumberOfGCLogFiles产生更多的GC日志时(在本例中是5个),gc.log.0中的旧GC日志内容将被删除。新的GC事件将被写入gc.log.0。这意味着你最终将无法获得所有生成的GC日志。你将失去所有事件的可见性

Mixed-Up GC Logs

Suppose an application has created five GC log files, including:

假设一个应用程序创建了五个GC日志文件,包括:

  • gc.log.0
  • gc.log.1
  • gc.log.2
  • gc.log.3
  • gc.log.4

Then, let’s say you are restarting the application. Now, new GC logs will be written to gc.log.0 file and old GC log content will be present in gc.log.1gc.log.2gc.log.3gc.log.4, etc.

然后,假设你正在重启应用程序。现在新的GC日志将被写入gc.log.0文件,而旧的GC日志内容将出现在gc.log.1gc.log.2gc.log.3gc.log.4

  • gc.log.0 ← GC log file content after restart
  • gc.log.1 ← GC log file content before restart
  • gc.log.2 ← GC log file content before restart
  • gc.log.3 ← GC log file content before restart
  • gc.log.4 ← GC log file content before restart

So, your new GC log contents get mixed up with old GC logs. Thus, to mitigate this problem, you might have to move all the old GC logs to a different folder before you restart the application.

所以新GC日志和旧的GC日志会混合在一起,为了缓解这个问题,你可能要在重启应用程序之前把所有旧的GC日志移到一个不同的文件夹里。

Forwarding GC Logs to a Central Location(将GC日志转发到一个中心位置)

In this approach, the current active file to which GC logs are written is marked with the extension  .current. For example, if GC events are currently written to the file gc.log.3, it would be named as: gc.log.3.current.

在这种方法中,当前写入GC日志的活动文件被标记为扩展名.current。例如,如果GC事件当前被写入文件gc.log.3,它将被命名为。 gc.log.3.current

If you want to forward GC logs from each server to a central location, then most DevOps engineers use  rsyslog. However, this file naming convention poses a significant challenge to use rsyslog, as described in this blog.

如果你想把每台服务器的GC日志转发到一个中心位置,那么大多数DevOps工程师会使用rsyslog。然而,这种文件命名惯例给使用rsyslog带来了巨大的挑战,正如[本博客所述](http://www.planetcobalt.net/s...)。

Tooling

Now, to analyze the GC log file using the GC tools such as (GCeasy, GCViewer, etc.), you will have to upload multiple GC log files instead of just one single GC Log file.

现在,为了使用GC工具分析GC日志文件,如(GCeasy, GCViewer等),你将不得不上传多个GC日志文件,而不是只有一个GC日志文件。

Recommended Solution

We can suffix the GC log file with the time stamp at which the JVM was restarted, then the GC Log file locations will become unique. Then, new GC logs will not override the old GC logs. It can be achieved by suffixing %t to the GC log file name, as shown below:

我们可以在GC日志文件后缀上JVM重启的时间戳(解决这个问题),那么GC日志文件的位置将变得独一无二。然后新的GC日志就不会覆盖旧的GC日志了。这可以通过在GC日志文件名后缀%t来实现,如下所示。

"-XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/home/GCEASY/gc-%t.log"

 %t suffixes timestamp to the GC log file in the format:  YYYY-MM-DD_HH-MM-SS. So, the generated GC log file name will start to look like: gc-2019-01-29_20-41-47.log.

 %t后缀为GC日志文件的时间戳,格式为:  yyyy-mm-dd_hh-mm-ss。因此,生成的GC日志文件名将开始看起来像: gc-2019-01-29_20-41-47.log.

This simple solution addresses all the shortcomings of -XX:+UseGCLogFileRotation.

这个简单的解决方案解决了-XX:+UseGCLogFileRotation的所有缺点。


技术读书笔记
个人阅读过的技术数据整理

赐他一块白石,石上写着新名

172 声望
34 粉丝
0 条评论
推荐阅读
《跟闪电侠学Netty》阅读笔记 - 开篇入门Netty
和 《Netty In Action》 不同,这本书直接从Netty入门程序代码开始引入Netty框架,前半部分教你如何用Netty搭建简易的通讯系统,整体难度比较低,后半部分直接从服务端源码、客户端源码、ChannelPipeline开始介绍...

Xander阅读 639

jmap执行失败了,怎么获取heapdump?
但当我反复模拟OOM场景测试时,发现jmap有时可以dump成功,有时会报错,如下: 经过网上一顿搜索,发现两种原因可能导致这个问题,一是执行jmap用户与jvm进程用户不一致,二是/tmp/.java_pidXXX文件被删除,但经...

扣钉日记2阅读 452

封面图
从原理聊JVM(一):染色标记和垃圾回收算法
属于共享内存区域,存储已被虚拟机加载的类信息、常量、静态变量、即时编译器编译后的代码等数据。运行时常量池,属于方法区的一部分,用于存放编译期生成的各种字面量和符号引用。

京东云开发者3阅读 414

封面图
线上FullGC问题排查实践——手把手教你排查线上问题 | 京东云技术团队
观察该机器日志发现,此时有很多线程在执行跑批任务。正常来说,跑批任务是低CPU高内存型,所以此时考虑是FullGC引起的大量CPU占用(之前有类似情况,告知用户后重启应用后解决问题)。

京东云开发者2阅读 352

封面图
java获取到heapdump文件后,如何快速分析?
在之前的OOM问题复盘之后,本周,又一Java服务出现了内存问题,这次问题不严重,只会触发堆内存占用高报警,没有触发OOM,但好在之前的复盘中总结了dump脚本,会在堆占用高时自动执行jstack与jmap,使得我们成功...

扣钉日记2阅读 741

封面图
一次线上OOM问题的个人复盘
上个月,我们一个java服务上线后,偶尔会发生内存OOM(Out Of Memory)问题,但由于OOM导致服务不响应请求,健康检查多次不通过,最后部署平台kill了java进程,这导致定位这次OOM问题也变得困难起来。

扣钉日记阅读 952

封面图
Class文件结构
Java类文件是包含可在Java 虚拟机 (JVM)上执行的Java 字节码的文件(具有.class 文件扩展名)。Java 类文件通常由Java 编译器根据包含 Java 类的 Java 编程语言源文件(.java文件)生成(或者,其他JVM 语言也可...

Zeran2阅读 392

封面图

赐他一块白石,石上写着新名

172 声望
34 粉丝
宣传栏