Wednesday 8 August 2018

Jcmd: GC.rotate_log: Rotate the log file

jcmd provides 'GC.rotate_log' command, it forces the the GC log file to be rotated.

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 HelloWorld.java application.

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

Open another command prompt and execute the command jcmd.
C:\>jcmd
18176 sun.tools.jcmd.JCmd
13220
12076 HelloWorld

As you see HelloWorld application is running with process id 12076.


Execute the command 'jcmd 12076 help' to see list of all the available commands available with process id 12076.


As you see above screen, 'GC.rotate_log' command is available to this process.

Now execute the command jcmd 12076 GC.rotate_log to force the log file rotation process.
C:\>jcmd 12076 GC.rotate_log
12076:
2018-07-26 14:35:53 GC log rotation request has been received. Saved as GCLog.0
2018-07-26 14:35:53 GC log file created GCLog.1



Previous                                                 Next                                                 Home

No comments:

Post a Comment