【JVM】Try to Avoid -XX UseGCLogFileRotation

简介: 【JVM】Try to Avoid -XX UseGCLogFileRotation

Try to Avoid -XX:+UseGCLogFileRotation

Source:dzone.com/articles/tr…

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.1, gc.log.2, gc.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。这意味着会存在旧日志文件的覆盖现象,将失去所有日志的可见性。

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.1, gc.log.2, gc.log.3, gc.log.4, etc.

然后,假设你正在重启应用程序。现在新的GC日志将被写入gc.log.0文件,而旧的GC日志内容将出现在gc.log.1、gc.log.2、gc.log.3、gc.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带来了巨大的挑战,正如这篇博客所述。

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的所有缺点。



相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
Java
揭开JVM所看到的try/catch/finally
#揭开JVM所看到的try/catch/finally 最近有一位朋友发了一段代码给我,这个方法很简单,具体内容大致如下: ```java int num = 5000000;//500万 long begin = System.currentTimeMillis(); for(int i=0; i
1974 0
|
Arthas 存储 算法
深入理解JVM,包含字节码文件,内存结构,垃圾回收,类的声明周期,类加载器
JVM全称是Java Virtual Machine-Java虚拟机JVM作用:本质上是一个运行在计算机上的程序,职责是运行Java字节码文件,编译为机器码交由计算机运行类的生命周期概述:类的生命周期描述了一个类加载,使用,卸载的整个过类的生命周期阶段:类的声明周期主要分为五个阶段:加载->连接->初始化->使用->卸载,其中连接中分为三个小阶段验证->准备->解析类加载器的定义:JVM提供类加载器给Java程序去获取类和接口字节码数据类加载器的作用:类加载器接受字节码文件。
1356 55
|
Arthas 监控 Java
Arthas memory(查看 JVM 内存信息)
Arthas memory(查看 JVM 内存信息)
1247 6
|
缓存 Prometheus 监控
Elasticsearch集群JVM调优设置合适的堆内存大小
Elasticsearch集群JVM调优设置合适的堆内存大小
3022 1
|
存储 缓存 Java
我们来说一说 JVM 的内存模型
我是小假 期待与你的下一次相遇 ~
808 5
|
存储 缓存 算法
深入理解JVM《JVM内存区域详解 - 世界的基石》
Java代码从编译到执行需经javac编译为.class字节码,再由JVM加载运行。JVM内存分为线程私有(程序计数器、虚拟机栈、本地方法栈)和线程共享(堆、方法区)区域,其中堆是GC主战场,方法区在JDK 8+演变为使用本地内存的元空间,直接内存则用于提升NIO性能,但可能引发OOM。
|
存储 设计模式 监控
快速定位并优化CPU 与 JVM 内存性能瓶颈
本文介绍了 Java 应用常见的 CPU & JVM 内存热点原因及优化思路。
1656 166
|
存储 缓存 算法
JVM简介—1.Java内存区域
本文详细介绍了Java虚拟机运行时数据区的各个方面,包括其定义、类型(如程序计数器、Java虚拟机栈、本地方法栈、Java堆、方法区和直接内存)及其作用。文中还探讨了各版本内存区域的变化、直接内存的使用、从线程角度分析Java内存区域、堆与栈的区别、对象创建步骤、对象内存布局及访问定位,并通过实例说明了常见内存溢出问题的原因和表现形式。这些内容帮助开发者深入理解Java内存管理机制,优化应用程序性能并解决潜在的内存问题。
962 29
JVM简介—1.Java内存区域
|
缓存 监控 算法
JVM简介—2.垃圾回收器和内存分配策略
本文介绍了Java垃圾回收机制的多个方面,包括垃圾回收概述、对象存活判断、引用类型介绍、垃圾收集算法、垃圾收集器设计、具体垃圾回收器详情、Stop The World现象、内存分配与回收策略、新生代配置演示、内存泄漏和溢出问题以及JDK提供的相关工具。
JVM简介—2.垃圾回收器和内存分配策略
|
存储 监控 算法
深入探索Java虚拟机(JVM)的内存管理机制
本文旨在为读者提供对Java虚拟机(JVM)内存管理机制的深入理解。通过详细解析JVM的内存结构、垃圾回收算法以及性能优化策略,本文不仅揭示了Java程序高效运行背后的原理,还为开发者提供了优化应用程序性能的实用技巧。不同于常规摘要仅概述文章大意,本文摘要将简要介绍JVM内存管理的关键点,为读者提供一个清晰的学习路线图。