在 Java 中滚动垃圾收集器日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3822097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rolling garbage collector logs in java
提问by Krzysztof Krasoń
Is it possible to do a rolling of garbage collector logs in Sun JVM?
是否可以在 Sun JVM 中滚动垃圾收集器日志?
Currently I generate logs using:
目前我使用以下方法生成日志:
-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -verbose:gc -Xloggc:gc.log
But I have to manually rotate them using fifo queues and rotatelogs to create a new log for each day. I hope that there is a better solution for this.
但是我必须使用 fifo 队列和 rotatelogs 手动轮换它们,以便为每一天创建一个新日志。我希望有一个更好的解决方案。
Maybe there is a way to access this log entries from inside java so I could redirect them to log4j?
也许有一种方法可以从 Java 内部访问此日志条目,以便我可以将它们重定向到 log4j?
Edit: the solution with fifo queue is not good enough because if the process that reads from this queue (e.g. rotatelogs) reads to slow it will slow down the entire jvm (apparently Sun/Oracle does gc logging synchronously)
编辑:使用 fifo 队列的解决方案不够好,因为如果从此队列读取的进程(例如rotatelogs)读取速度变慢,它将减慢整个 jvm(显然 Sun/Oracle 同步执行 gc 日志记录)
采纳答案by Johan Kaving
Built-in support for GC log rotation has been added to the HotSpot JVM. It is described in the RFE 6941923and is available in:
HotSpot JVM 中添加了对 GC 日志轮换的内置支持。它在RFE 6941923 中有描述,可在以下位置获得:
- Java 6 Update 34
- Java 7 Update 2(but there is no reference to it in these release notes)
- Java 6 更新 34
- Java 7 Update 2 (但在这些发行说明中没有提及它)
There are three new JVM flags that can be used to enable and configure it:
有三个新的 JVM 标志可用于启用和配置它:
-XX:+UseGCLogFileRotation
must be used with-Xloggc:<filename>
;-XX:NumberOfGCLogFiles=<number of files>
must be >=1, default is one;-XX:GCLogFileSize=<number>M (or K)
default will be set to 512K.
-XX:+UseGCLogFileRotation
必须与-Xloggc:<filename>
;-XX:NumberOfGCLogFiles=<number of files>
必须>=1,默认为1;-XX:GCLogFileSize=<number>M (or K)
默认将设置为 512K。
回答by Blazej
Have you tried this new options?
你试过这个新选项吗?
I tried jdk7u7, jdk7u6 and jdk6u35 like this:
我像这样尝试了 jdk7u7、jdk7u6 和 jdk6u35:
java -Xloggc:gc.log -XX:+PrintGCDetails -XX:+UseGCLogRotation -XX:NumberOfGClogFiles=3 -XX:GCLogFileSize=10M
but with every version I'm seeing this error:
但是对于每个版本,我都会看到此错误:
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Bugfix #6941923 for 7u2 is referenced here: http://www.oracle.com/technetwork/java/javase/2col/7u2bugfixes-1394661.html
此处引用了 7u2 的错误修复 #6941923:http: //www.oracle.com/technetwork/java/javase/2col/7u2bugfixes-1394661.html
回答by Underverse
If you can't upgrade your java version to use the new flags for rotating the gc log then you could specify a different gc file each time the application starts:
如果您无法升级 Java 版本以使用新标志来轮换 gc 日志,那么您可以在每次应用程序启动时指定不同的 gc 文件:
JAVA_OPTS="-Xms1024m -Xmx1024m -XX:MaxPermSize=256m -verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:/path/to/log/dir/gc.log-"`date +%Y-%m-%d-%H-%M`
When the setenv is referenced, usually at startup or shutdown, it will reference a different log file. In unix this can be used as a method for 'rotating' the log.
当 setenv 被引用时,通常在启动或关闭时,它会引用不同的日志文件。在 unix 中,这可以用作“旋转”日志的方法。
回答by Sergey Shcherbakov
An interesting approach would be to redirect gc.log to a named pipe
-Xloggc:/my/named/pipe
How to write GC log to named pipe
一个有趣的方法是将 gc.log 重定向到命名管道
-Xloggc:/my/named/pipe
How to write GC log to named pipe
then read that pipe form the application itself: How to open a Windows named pipe from Java?
然后从应用程序本身读取该管道: How to open a Windows named pipe from Java?
and log to an arbitrary (e.g. async rolling) logback logger from the code.
并从代码登录到任意(例如异步滚动)logback 记录器。
Tried that on a Windows machine. Unfortunately, it is trickier to setup on Windows than on Linux.
在 Windows 机器上试过了。不幸的是,在 Windows 上设置比在 Linux 上更棘手。
On Windows it works basically with help of an additional Powershell script(can be a dedicated application as well). This sample projectalso contains a demo application that can be used right away to test the GC logs redirection to Logback via SLF4J.
在 Windows 上,它基本上可以在额外的Powershell 脚本(也可以是专用应用程序)的帮助下工作。此示例项目还包含一个演示应用程序,可立即用于测试通过 SLF4J 重定向到 Logback 的 GC 日志。
回答by ajay
I ended up solving this problem by spawning a new thread in my application and sending jcmdlog-rotate command periodically (based on a cron expression).
我最终通过在我的应用程序中生成一个新线程并定期发送jcmdlog-rotate 命令(基于 cron 表达式)来解决这个问题。
This is an unconventional approach as you will be using Oracle's Attach API, although the approach served our use case of rotating GC logs hourly.
这是一种非常规方法,因为您将使用 Oracle 的Attach API,尽管该方法适用于我们每小时轮换 GC 日志的用例。
回答by Dharan
Using -XX:+UseGCLogFileRotation will cause some serious long safepoint issue with the solaris and JDK versions 1.7.0_80 - 1.7.0_97 and 1.8.0_20 - 1.8.0_77
使用 -XX:+UseGCLogFileRotation 将导致与 1.7.0_80 - 1.7.0_97 和 1.8.0_20 - 1.8.0_77 的solaris 和JDK 版本的一些严重的长期安全点问题