Wednesday 8 August 2018

Rotate GC Log files

In my previous post, I explained how to collect GC log files. In this post, I am going to show you how to rotate GC log files.

It is quite simple, launch the application using below vm arguments.

-XX:NumberOfGCLogFiles=5: Number of log files is 5
-XX:GCLogFileSize=2M: Maximum file size is 2 MB. Once the log file size is reached to 2MB, it creates new log file.

HelloWorld.java
import java.util.ArrayList;
import java.util.List;

public class HelloWorld {
 public static void main(String args[]) throws Exception {
  Thread t1 = new Thread() {
   public void run() {
    while (true) {
     List<Integer> list = new ArrayList<>();

     for (int i = 0; i < 10000000; i++) {
      list.add(i);
     }
     try {
      Thread.sleep(5000);
     } catch (Exception e) {

     }
    }
   }
  };

  t1.start();
 }
}


Compile the HelloWorld.java application.

Run the applicaiton using below command.
java -classpath . -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:GCLog -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M HelloWorld

You can able to see the file names like below.

GCLog.0
GCLog.1
GCLog.2
GCLog.3
GCLog.4

Most recent GC log contents will be written to 'GCLog.4' and old GC log content will be present in 'GCLog.0'.




Previous                                                 Next                                                 Home

No comments:

Post a Comment