原文(需翻墙):http://javaeesupportpatterns.blogspot.com/2011/10/verbosegc-output-tutorial-java-7.html
Verbose output for the Java VM and garbage collection process has been around for quite a long time but I often get questions on how to read and interpret the output.
This short article will provide with a detailed tutorial on how to enable and read Java 7 HotSpot VM verbose gc output data.
I recommend that you compile and run the sample program on your end as well.
Tutorial video available
We also recently created a Java verbose GC tutorial video explaining this analysis process.
package org.ph.javaee.tools.jdk7;
import java.util.Map;
import java.util.HashMap;
/**
* JavaHeapVerboseGCTest
* @author Pierre-Hugues Charbonneau
*
*/
public class JavaHeapVerboseGCTest {
private static Map<String, String> mapContainer = new HashMap<String, String>();
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Java 7 HotSpot Verbose GC Test Program v1.0");
System.out.println("Author: Pierre-Hugues Charbonneau");
System.out.println("http://javaeesupportpatterns.blogspot.com/");
String stringDataPrefix = "stringDataPrefix";
// Load Java Heap with 3 M java.lang.String instances
for (int i=0; i<3000000; i++) {
String newStringData = stringDataPrefix + i;
mapContainer.put(newStringData, newStringData);
}
System.out.println("MAP size: "+mapContainer.size());
System.gc(); // Explicit GC!
// Remove 2 M out of 3 M
for (int i=0; i<2000000; i++) {
String newStringData = stringDataPrefix + i;
mapContainer.remove(newStringData);
}
System.out.println("MAP size: "+mapContainer.size());
System.gc();
System.out.println("End of program!");
}
}
Step #2 – Enable verbose GC via the JVM start-up arguments
As you can see from the verbose GC output, the OldGen space was at 340 MB after the initial loading of 3 M String instances in our HashMap. It did go down to 126 MB following the removal of 2 M String instances.
Now find below explanation and snapshots on how you can read the GC output data in more detail for each Java Heap space.
## YoungGen space analysis
## OldGen space analysis
## PermGen space analysis
## Java Heap breakdown analysis