We can use the Java command line and UI tools to monitor the application's garbage collection activities. In the following example, I use a demo program in Java SE Downloads.
If you want to use the same program, go to the Java SE Downloads page to download JDK 7 and JavaFX Demos and Samples. The example I used and ordered is Java2demo.jar can be found in the jdk1.7.0_55/demo/jfc/java2d directory. Of course, this step is optional. You can use any Java program to perform GC monitoring commands.
The commands I use to start the demo program are:
[Email protected]: ~/downloads/jdk1.7.0_55/demo/jfc/java2d$ java-xmx120m-xms30m-xmn10m-xx:permsize=20m-xx: Maxpermsize=20m-xx:+useserialgc-jar Java2demo.jar
1, JSTAT
We can use the Jstat command-line tool to monitor JVM memory and GC activity. This command is included in the standard JDK. So it can be used directly.
Before running jstat you need to know the process ID number of the program. You can run Ps-eaf | grep java command to get.
[Email protected]: ~$ ps-eaf | grep java2demo.jar501 9582 11579 0 9:48pm ttys000 0:21.66/usr/bin/java-xmx120m-xms30m-xmn10m-xx: Permsize=20m-xx:maxpermsize=20m-xx:+useg1gc-jar java2demo.jar501 14073 14045 0 9:48pm ttys002 0:00.00 grep Java2demo.jar
The process ID number for my Java program is 9582. Now we can execute the jstat command as follows.
1 [email protected]: ~$ jstat-gc 9582 10002 s0c s1c s0u s1u EC EU OC OU PC PU YGC ygct FGC fgct GCT3 1024.0 1024.0 0.0 0.0 8192.0 7933.3 42108.0 23401.3 20480. 0 19990.9 157 0.274 40 1.381 1.6544 1024.0 1024.0 0.0 0.0 8192.0 8026.5 42108.0 23401.3 204 80.0 19990.9 157 0.274 40 1.381 1.6545 1024.0 1024.0 0.0 0.0 8192.0 8030.0 42108.0 23401.3 20480.0 19990.9 157 0.274 40 1.381 1.6546 1024.0 1024.0 0.0 0.0 8192.0 8122.2 42108.0 23401.3 20480.0 19990.9 157 0.274 40 1.381 1.6547 1024.0 1024.0 0.0 0.0 8192.0 8171.2 42108.0 2340 1.3 20480.0 19990.9 157 0.274 40 1.381 1.6548 1024.0 1024.0 48.7 0.0 8192.0 106.7 42108.0 2 3401.3 20480.0 19990.9 158 0.275 40 1.381 1.6569 1024.0 1024.0 48.7 0.0 8192.0 145.8 42108.0 23401.3 20480.0 19990.9 158 0.275 40 1.381 1.656
The last parameter of the Jstat command is the time interval for each output, so it prints memory and garbage collection data every second. Look at each column in more detail below.
- s0c and s1c: This column shows the size (in kilobytes)of the current Survivor0 and Survivor1 area.
- s0u and s1u: This column shows the usage of the Survivor0 and Survivor1 region (KB). One of the survivor areas is always empty.
- EC and EU: This column shows the current size and usage of the Eden Zone (in kilobytes). Note that the size of the EU increases gradually when the EC size is reached, minimizing the GC is called, and the size of the EU decreases.
- OC and OU: This column shows the current size and usage of the old age (KB).
- PC and PU: This column shows the current size and usage of the persistence generation (Perm Gen) (KB).
- YGC and YGCT: The YGC column shows the number of GC events occurring in the young generation. The YGCT column shows the cumulative time of GC operations occurring in the young generation. Note that both column values are increasing in the same row as the EU value reduction. This is primarily the reason for minimizing GC.
- FGC and FGCT: The FGC column shows the number of full GC occurrences. The FGCT column shows the cumulative time of the full GC operation. Note the time spent in full GC is much larger than the time spent in a younger generation of GC.
- GCT: This column shows the total cumulative time for GC operations. Note that it is the sum of the YGCT and FGCT values.
The advantage of Jstat is that it can be executed on a remote server without a GUI. Note that the sum of s0c, S1C, and EC is 10M, just as we set the value through the JVM option-xmn10m.
2. Java VisualVM with Visual GC
If you want to view memory and GC operations under the GUI. Then you can use the JVISUALVM tool. Java VisualVM is also included in the JDK and you do not need to download it separately.
You only need to run the JVISUALVM command on the terminal to start the Java VISUALVM program. Once started, you need to install the Visual GC plugin via the tools--"plugins option, as shown in.
After the visual GC installation is complete, open the program to the Visual GC section in the left column. You will get the JVM memory and GC as shown.
Java GC tuning is the last option to increase application throughput, and only when you find that a long GC results in a performance degradation that results in an application timeout.
You will see the Java.lang.OutOfMemoryError:PermGen space error message in the log. You can then try to monitor and increase the perm Gen memory space by using the JVM option-xx:permgen And-xx:maxpermgen. You might also want to try using-xx:+cmsclassunloadingenabled to check the performance of using CMS garbage collection?
If you find a lot of full GC operations, you can try to increase the old generation of memory space.
In short, GC tuning takes a lot of effort and time, and there's absolutely no hard or fast rule here. You need to try different options, compare them, and find the one that's best for your application.
Transferred from: http://www.cnblogs.com/tonyspark/p/3731696.html
JAVA Garbage Collection Monitoring