How can I create rolling gc.log files?
Environment
- Red Hat Enterprise Linux (RHEL)
- 7.x
- OpenJDK 8
- OpenJDK 11
Issue
- The JVM's are infrequently started and our GC files can get extremely large. I would like to be able to create "daily" gc logs somehow without having to restart the JVM. It would be nice if my nightly script could somehow detach the gc file from the running JVM process, create a new GC file and then somehow re-attach the file to the existing JVM process.
- The size of the gc log is increasing. How can we reduce or partition gc logs? Or can we disable that logging?
- How to rotate the gc.log in JBoss?
Resolution
Disclaimer: Links contained herein to an external website(s) are provided for convenience only. Red Hat has not reviewed the links and is not responsible for the content or its availability. The inclusion of any link to an external website does not imply endorsement by Red Hat of the website or their entities, products or services. You agree that Red Hat is not responsible or liable for any loss or expenses that may result due to your use of (or reliance on) the external site or content.
JDK 11
- In Java 11, the
gc.logcan be rolled through following JVM settings:
-verbose:gc -Xlog:gc*:file=<file>::filecount=<count>,filesize=<filesize in kb>
JDK 8
- The gc.log can be rolled through JVM settings
UseGCLogFileRotation/NumberOfGCLogFiles/GCLogFileSizeon the OpenJDK 8. For example:
-verbose:gc -Xloggc:gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=1048576
- Note:
- This feature is available in OpenJDK 6 from the following release in RHEL 5, RHEL 6 by this [BZ](https://bugzilla.redhat.com/show_bug.cgi?id=988964). Also this feature is available in OpenJDK 6 included RHEL 7 GA.
- RHEL 5: java-1.6.0-openjdk-1.6.0.0-3.1.13.1.el5_10 or later
- RHEL 6: java-1.6.0-openjdk-1.6.0.0-1.66.1.13.0.el6 or later
- `-Xloggc:<filename>` : Log GC verbose output to specified file. The verbose output is controlled by the normal verbose GC flags.
- `-XX:+UseGCLogFileRotation` : Enabled GC log rotation, requires `-Xloggc`.
- `-XX:NumberOfGClogFiles` : Set the number of files to use when rotating logs, must be >= 1. The rotated log files will use the following naming scheme, `<filename>.0`, `<filename>.1`, ..., `<filename>.n-1`.
- `-XX:GCLogFileSize` : The size of the log file at which point the log will be rotated, must be >= 8K. You can specify `-XXGCLogFileSize=100K` or `-XXGCLogFileSize=1M` format. The smallest value of `-XX:GCLogFileSize` you can specify is 8K.
- Please also refer to [http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html](http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html)
- The gc.log can be rolled through JVM settings on the Content from www-01.ibm.com is not included.IBM JDK
Log Splitter option
-
If you are not on the IBM JDK or the latest OpenJDK and Oracle JDK, you can use a named pipe for the GC logging output, and have another process read that and log into appropriately named files. For example:
mkfifo gclog-$SERVER_NAME.pipe logsplitter.sh gclog-$SERVER_NAME.log < gclog-$SERVER_NAME.pipe & JAVA_OPTS="$JAVA_OPTS -Xloggc:gclog-$SERVER_NAME.pipe"Example logsplitter.sh:
#!/bin/bash OUTPUT_PREFIX=$1 while true; do read LINE STATUS=$? if [[ $STATUS -ne 0 ]]; then # got EOF break; fi DATE=`date +%F` echo $LINE >> "$OUTPUT_PREFIX-$DATE" doneThe example script above needs better error handling, because if it dies the named pipe will eventually fill up and the JVM will pause waiting to write out the GC logging. To prevent/mitigate the effects of this, you could establish another back up script, for example this logsplitterchecker.sh:
#!/bin/bash LOGSPLITTERPID=$(pgrep logsplitter.sh) if [[ -z "$LOGSPLITTERPID" ]]; then /path/to/logsplitter.sh /path/to/gclog-$SERVER_NAME.log < /path/to/gclog-$SERVER_NAME.pipe fi unset LOGSPLITTERPIDThis script will check for an existing process that is running logsplitter.sh. If one does not exist, it will start executing logsplitter.sh again. You could then set this logsplitterchecker.sh to run however often you want through a cronjob, for example this one for once a minute:
* * * * * /path/to/logsplitterchecker.sh
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.