Build a jar package
Using Spring Boot to build a simple web program, you can start it directly using java jar.
@RestController @RequestMapping("/root") @SpringBootApplication public class SbDemoApplication { public static void main(String[] args) { SpringApplication.run(SbDemoApplication.class, args); } @RequestMapping("/hello") public String hello() { return "Hello"; } }
start-up
Use the two parameters - verbose:gc or - XX:+PrintGC to create basic GC logs, and - XX:+PrintGCDetails to create more detailed logs.
$ java -jar -XX:+PrintGCDetails sb-demo.jar [0.002s][warning][gc] -XX:+PrintGCDetails is deprecated. Will use -Xlog:gc* instead. [0.009s][info ][gc,heap] Heap region size: 1M [0.013s][info ][gc ] Using G1 [0.013s][info ][gc,heap,coops] Heap address: 0x0000000740000000, size: 2048 MB, Compressed Oops mode: Zero based, Oop shift amount: 3
Start sb-demo.jar and find that - XX:+PrintGCDetails is not recommended. Use - Xlog:gc *. Then I have to go to Oracle's website to find out how to use Xlog. I use JDK 10, so I'll check this version Tools and Commands Reference .
$ java -jar -Xlog:gc sb-demo.jar [0.010s][info][gc] Using G1 [0.492s][info][gc] GC(0) Pause Young (G1 Evacuation Pause) 14M->2M(128M) 5.058ms
Use the above command to open the gc log. The default is info level. You can modify the level of gc log in the following way
$ java -jar -Xlog:gc=debug sb-demo.jar [0.007s][debug][gc] ConcGCThreads: 1 [0.007s][debug][gc] ParallelGCThreads: 4 [0.008s][debug][gc] Initialize mark stack with 4096 chunks, maximum 16384 [0.010s][info ][gc] Using G1 [0.499s][info ][gc] GC(0) Pause Young (G1 Evacuation Pause) 14M->2M(128M) 4.556ms
The above method can only see the print log in the standard output. We can set the gc log output to the file, which is convenient for us to analyze the problem.
$ java -jar -Xlog:gc=debug:file=gc.log sb-demo.jar
Using the above method, the GC log is output to the file gc.log.
$ head gc.log [0.008s][debug][gc] ConcGCThreads: 1 [0.008s][debug][gc] ParallelGCThreads: 4 [0.008s][debug][gc] Initialize mark stack with 4096 chunks, maximum 16384 [0.010s][info ][gc] Using G1
When the service runs all the time, gc.log will continue to increase and eventually fill our disk. We can set the file size and number of logs
$ java -jar -Xlog:gc=debug:file=gc.log,filecount=5,filesize=1024 sb-demo.jar
The above settings only output 5 files, each of which is 1M
Analyze gc log
You can directly open the log file to view or upload it to some gc log analysis websites and directly generate reports. As for these online Java gc log analysis websites, you can google some and have many choices for you.