Java 如何将详细垃圾收集输出重定向到文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1161647/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:24:03  来源:igfitidea点击:

How to redirect verbose garbage collection output to a file?

garbage-collectionjavajdk1.5

提问by djangofan

How do I redirect verbose garbage collection output to a file? Sun's website shows an example for Unix but it doesn't work for Windows.

如何将详细垃圾收集输出重定向到文件?Sun 的网站显示了一个 Unix 示例,但它不适用于 Windows。

采纳答案by Michael Myers

From the output of java -X:

从输出java -X

    -Xloggc:<file>    log GC status to a file with time stamps

Documented here:

记录在这里

-Xloggc:filename

Sets the file to which verbose GC events information should be redirected for logging. The information written to this file is similar to the output of -verbose:gcwith the time elapsed since the first GC event preceding each logged event. The -Xloggcoption overrides -verbose:gcif both are given with the same javacommand.

Example:

    -Xloggc:garbage-collection.log

-Xloggc:文件名

设置应将详细 GC 事件信息重定向到哪个文件以进行日志记录。写入此文件的信息类似于-verbose:gc自每个记录事件之前的第一个 GC 事件以来经过的时间的输出。如果两者都使用相同的命令给出,则该-Xloggc选项会覆盖。-verbose:gcjava

例子:

    -Xloggc:garbage-collection.log

So the output looks something like this:

所以输出看起来像这样:

0.590: [GC 896K->278K(5056K), 0.0096650 secs]
0.906: [GC 1174K->774K(5056K), 0.0106856 secs]
1.320: [GC 1670K->1009K(5056K), 0.0101132 secs]
1.459: [GC 1902K->1055K(5056K), 0.0030196 secs]
1.600: [GC 1951K->1161K(5056K), 0.0032375 secs]
1.686: [GC 1805K->1238K(5056K), 0.0034732 secs]
1.690: [Full GC 1238K->1238K(5056K), 0.0631661 secs]
1.874: [GC 62133K->61257K(65060K), 0.0014464 secs]

回答by Marc Giombetti

If in addition you want to pipe the output to a separate file, you can do:

此外,如果您想将输出通过管道传输到一个单独的文件,您可以执行以下操作:

On a Sun JVM:

Sun JVM 上:

-Xloggc:C:\whereever\jvm.log -verbose:gc -XX:+PrintGCDateStamps

ON an IBM JVM:

IBM JVM 上:

-Xverbosegclog:C:\whereever\jvm.log 

回答by 18446744073709551615

To add to the above answers, there's a good article: Useful JVM Flags – Part 8 (GC Logging)by Patrick Peschlow.

除了上述答案之外,还有一篇很好的文章:Patrick Peschlow 的有用的 JVM 标志 – 第 8 部分(GC 日志记录)

A brief excerpt:

一个简短的摘录:

The flag -XX:+PrintGC(or the alias -verbose:gc) activates the “simple” GC logging mode

标志-XX:+PrintGC(或别名-verbose:gc)激活“简单”GC 日志记录模式

By default the GC log is written to stdout. With -Xloggc:<file>we may instead specify an output file. Note that this flag implicitly sets -XX:+PrintGCand -XX:+PrintGCTimeStampsas well.

默认情况下,GC 日志写入标准输出。随着-Xloggc:<file>我们可以改为指定输出文件。请注意,这个标志隐式设置-XX:+PrintGC,并-XX:+PrintGCTimeStamps为好。

If we use -XX:+PrintGCDetailsinstead of -XX:+PrintGC, we activate the “detailed” GC logging mode which differs depending on the GC algorithm used.

如果我们使用-XX:+PrintGCDetails代替-XX:+PrintGC,我们会激活“详细”GC 日志记录模式,该模式因所使用的 GC 算法而异。

With -XX:+PrintGCTimeStampsa timestamp reflecting the real time passed in seconds since JVM start is added to every line.

随着-XX:+PrintGCTimeStamps时间戳反映,因为JVM开始在几秒钟内通过实时添加到每一行。

If we specify -XX:+PrintGCDateStampseach line starts with the absolute date and time.

如果我们指定-XX:+PrintGCDateStamps每一行以绝对日期和时间开始。

回答by Ali Dehghani

Java 9 & Unified JVM Logging

Java 9 和统一的 JVM 日志记录

JEP 158introduces a common logging system for all components of the JVM which will change (and IMO simplify) how logging works with GC. JEP 158 added a new command-line option to control logging from all components of the JVM:

JEP 158为 JVM 的所有组件引入了一个通用日志系统,这将改变(并简化 IMO)日志记录与 GC 的工作方式。JEP 158 添加了一个新的命令行选项来控制来自 JVM 的所有组件的日志记录:

-Xlog

For example, the following option:

例如,以下选项:

-Xlog:gc

will log messages tagged with gctag using infolevel to stdout. Or this one:

gc使用infolevel to记录用tag 标记的消息stdout。或者这个:

-Xlog:gc=debug:file=gc.txt:none

would log messages tagged with gctag using debuglevel to a file called gc.txtwith no decorations. For more detailed discussion, you can checkout the examples in the JEPpage.

会将gc使用debuglevel标记为tag 的消息记录到一个gc.txt没有装饰的文件中。有关更详细的讨论,您可以查看JEP页面中的示例。