原文
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+的區別,包括如何將輸出重定向到文件等操作。