Try to Avoid -XX:+UseGCLogFileRotation
Source:https://dzone.com/articles/try-to-avoid-xxusegclogfilerotation
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的所有缺點。