Rolling garbage collector logs in java

Viewed 46119

Is it possible to do a rolling of garbage collector logs in Sun JVM?

Currently I generate logs using:

-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -verbose:gc -Xloggc:gc.log 

But I have to manually rotate them using fifo queues and rotatelogs to create a new log for each day. I hope that there is a better solution for this.

Maybe there is a way to access this log entries from inside java so I could redirect them to log4j?

Edit: the solution with fifo queue is not good enough because if the process that reads from this queue (e.g. rotatelogs) reads to slow it will slow down the entire jvm (apparently Sun/Oracle does gc logging synchronously)

6 Answers

I ended up solving this problem by spawning a new thread in my application and sending jcmd log-rotate command periodically (based on a cron expression).

This is an unconventional approach as you will be using Oracle's Attach API, although the approach served our use case of rotating GC logs hourly.

Using -XX:+UseGCLogFileRotation will cause some serious long safepoint issue with the solaris and JDK versions 1.7.0_80 - 1.7.0_97 and 1.8.0_20 - 1.8.0_77

Related