关于java:JavaGarbage-Collection-Logging-to-a-File-in-Java

6次阅读

共计 3917 个字符,预计需要花费 10 分钟才能阅读完成。

原文

https://www.baeldung.com/java-gc-logging-to-file

1. Overview

Garbage collection is a marvel of the Java programming language providing us with automatic memory management.

垃圾回收是 Java 编程语言的一个奇观,它为咱们提供了主动内存治理性能。

Garbage collection hides the details of having to manually allocate and deallocate memory.

垃圾回收暗藏了手动调配和删除内存的细节。

While this mechanism is fantastic, sometimes it doesn’t work the way we want.

尽管这种机制十分好,但有时并不能如咱们所愿。

In this tutorial, we’ll explore Java’s logging options for garbage collection statistics and discover how to redirect these statistics to a file.

在本教程中,咱们将摸索 Java 的 垃圾收集统计日志选项 ,并理解如何 将这些统计信息重定向到文件

2. GC Logging Flags in Java 8 and Earlier[](https://www.baeldung.com/java-gc-logging-to-file#gc-logging-f…)

First, let’s explore the JVM flags relating to GC logging in Java versions prior to Java 9.

首先,让咱们探讨 Java 9 之前的 Java 版本中与 GC 日志相干的 JVM 标记。

2.1. _-XX:+PrintGC_

The _-XX:+PrintGC_ flag is an alias for _-verbose:gc_ and turns on basic GC logging.

-XX:+PrintGC 标记是_-verbose:gc_的别名,作用是 开启根本的 GC 日志

In this mode, a single line is printed for every young-generation and every full-generation collection.

在这个模式中,每个年老代和残缺代的收集操作都会打印一行。

Let’s now turn our attention to providing detailed GC information.

当初,让咱们把注意力转向提供具体的 GC 信息。

2.2. _-XX:+PrintGCDetails_

Similarly, we have the flag _-XX:+PrintGCDetails_ used to activate detailed GC logging instead of _-XX:+PrintGC_.

同样,咱们应用标记 -XX:+PrintGCDetails 激活具体的 GC 日志,而不是 _-XX:+PrintGC_。

Note that the output from _-XX:+PrintGCDetails_ changes depending on the GC algorithm in use.

请留神,_-XX:+PrintGCDetails_ 的输入会依据应用的 GC 算法而扭转。

Next, we’ll look at annotating our logs with date and time information.

接下来,咱们将理解如何用日期和工夫信息来正文日志。

2.3. _-XX:+PrintGCDateStamps_ and _-XX:+PrintGCTimeStamps_

We can add dates and timing information to our GC logs by utilizing the flags _-XX:+PrintGCDateStamps_ and _-XX:+PrintGCTimeStamps_, respectively.

咱们能够别离利用标记 -XX:+PrintGCDateStamps-XX:+PrintGCTimeStamps 在 GC 日志中 ** 增加日期和工夫信息。

First, _-XX:+PrintGCDateStamps_ adds the date and time of the log entry to the beginning of each line.

首先,_-XX:+PrintGCDateStamps_ 会在每行结尾增加日志条目标日期和工夫。

Second, _-XX:PrintGCTimeStamps_ adds a timestamp to every line of the log detailing the time passed (in seconds) since the JVM was started.

其次,_-XX:PrintGCTimeStamps_ 会在日志的每一行增加一个工夫戳,具体记录 JVM 启动后的工夫(以秒为单位)。

2.4. _-Xloggc_

Finally, we come to redirecting the GC log to a file.

最初,咱们来 将 GC 日志重定向到文件

This flag takes an optional filename as an argument using the syntax _-Xloggc:file_ and without the presence of a file name the GC log is written to standard out.

该标记应用 -Xloggc:file 语法将一个可选的文件名作为参数,如果没有文件名,GC 日志将被写入规范输入。

Additionally, this flag also sets the _-XX:PrintGC_ and _-XX:PrintGCTimestamps_ flags for us. Let’s look at some examples:

此外,该标记还会为咱们设置 -XX:PrintGC-XX:PrintGCTimestamps 标记。让咱们来看几个例子:

If we want to write the GC log to standard output, we can run:

如果咱们想将 GC 日志写入规范输入,能够运行:

java -cp $CLASSPATH -Xloggc mypackage.MainClass

Or to write the GC log to a file, we would run:

如果要写入 GC 日志到一个文件,应用上面参数

java -cp $CLASSPATH -Xloggc:/tmp/gc.log mypackage.MainClass

3. GC Logging Flags in Java 9 and Later

In Java 9+, _-XX:PrintGC_, the alias for _-verbose:gc_, has been deprecated in favor of the unified logging option, _-Xlog_.

Java9+ 的版本,_-Xlog_ 取代了  _-XX:PrintGC_ 和别名 _-verbose:gc_,此前的两个参数均曾经标记为“已弃用”

All other GC flags mentioned above are still valid in Java 9+.

当然,尽管被标记弃用,然而上述所有其余 GC 标记在 Java 9+ 中依然无效。

This new logging option allows us to specify which messages should be shown, set the log level, and redirect the output.

这个新的日志选项容许咱们 指定应显示哪些信息、设置日志级别和重定向输入

We can run the below command to see all the available options for log levels, log decorators, and tag sets:

咱们能够应用上面的命令查看所有可用的参数,比方日志等级,日志装璜器和标记集:

java -Xlog:logging=debug -version

For example, if we wanted to log all GC messages to a file, we would run:

如果咱们想要记录 GC 日志到一个内部文件,能够应用上面的启动参数。

java -cp $CLASSPATH -Xlog:gc*=debug:file=/tmp/gc.log mypackage.MainClass

Additionally, this new unified logging flag is repeatable, so you can, for example, log all GC messages to both standard out and a file:

此外,这一新的对立日志标记(-Xlog:gc)是可反复的,因而您能够 将所有 GC 音讯同时记录到规范输入和文件 中:

java -cp $CLASSPATH -Xlog:gc*=debug:stdout -Xlog:gc*=debug:file=/tmp/gc.log mypackage.MainClass

4. Conclusion

In this article, we’ve shown how to log garbage collection output in both Java 8 and Java 9+ including how to redirect that output to a file.

本节内容展现了开启收集打印日志在 JDK8 和 JDK9+ 的区别,包含如何将输入重定向到文件等操作。

正文完
 0