How to prevent Garbage Collection (GC) logs from filling up filesystem and generating "No space left on device" error?
GC log size will be directly proportional to the number of GC cycles. There can be cases where heap is near exhaustion and there are back-to-back GC cycles to try to keep up. In such situations, the GC log can become quite large.
There are 2 things that can be done to help prevent the GC log from taking up too much disk space:
- Firstly, to reduce the amount of GC logging, please remove
-XX:PrintGCDetailsstartup flag if present. This will still provide sufficient logging to debug GC issues, but significantly reduce the size of the logs.
- Secondly, it is recommend to set a maximum size for the GC log and also a maximum number of log files to keep. This way one can precisely define the maximize disk space the GC log will occupy (size x #). For example, setting the maximum size to 100MB with 5 log files (total of 500MB) can be accomplished with the following arguments:
-Xloggc:/home/java/gc-%t.log -XX:+GCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=100M
The above options will work across various versions of Java. Newer versions of Java can utilize Unified Logging. For example,