Rolling garbage collector logs in java

前端 未结 6 929
孤街浪徒
孤街浪徒 2020-12-08 04:12

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

Currently I generate logs using:

-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:         


        
相关标签:
6条回答
  • 2020-12-08 04:36

    An interesting approach would be to redirect gc.log to a named pipe -Xloggc:/my/named/pipe How to write GC log to named pipe

    then read that pipe form the application itself: How to open a Windows named pipe from Java?

    and log to an arbitrary (e.g. async rolling) logback logger from the code.

    Tried that on a Windows machine. Unfortunately, it is trickier to setup on Windows than on Linux.

    On Windows it works basically with help of an additional Powershell script (can be a dedicated application as well). This sample project also contains a demo application that can be used right away to test the GC logs redirection to Logback via SLF4J.

    0 讨论(0)
  • 2020-12-08 04:39

    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

    0 讨论(0)
  • 2020-12-08 04:40

    If you can't upgrade your java version to use the new flags for rotating the gc log then you could specify a different gc file each time the application starts:

    JAVA_OPTS="-Xms1024m -Xmx1024m -XX:MaxPermSize=256m -verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:/path/to/log/dir/gc.log-"`date +%Y-%m-%d-%H-%M`
    

    When the setenv is referenced, usually at startup or shutdown, it will reference a different log file. In unix this can be used as a method for 'rotating' the log.

    0 讨论(0)
  • 2020-12-08 04:42

    Built-in support for GC log rotation has been added to the HotSpot JVM. It is described in the RFE 6941923 and is available in:

    • Java 6 Update 34
    • Java 7 Update 2 (but there is no reference to it in these release notes)

    There are three new JVM flags that can be used to enable and configure it:

    • -XX:+UseGCLogFileRotation
      must be used with -Xloggc:<filename>;
    • -XX:NumberOfGCLogFiles=<number of files>
      must be >=1, default is one;
    • -XX:GCLogFileSize=<number>M (or K)
      default will be set to 512K.
    0 讨论(0)
  • 2020-12-08 04:45

    Have you tried this new options?

    I tried jdk7u7, jdk7u6 and jdk6u35 like this:

    java -Xloggc:gc.log -XX:+PrintGCDetails -XX:+UseGCLogRotation -XX:NumberOfGClogFiles=3 -XX:GCLogFileSize=10M
    

    but with every version I'm seeing this error:

    Error: Could not create the Java Virtual Machine.
    Error: A fatal exception has occurred. Program will exit.
    

    Bugfix #6941923 for 7u2 is referenced here: http://www.oracle.com/technetwork/java/javase/2col/7u2bugfixes-1394661.html

    0 讨论(0)
  • 2020-12-08 04:49

    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.

    0 讨论(0)
提交回复
热议问题