Skip to content

Commit 2af8ce7

Browse files
authored
Merge pull request eugenp#5940 from JonCook/master
BAEL-2175 - Verbose Garbage Collection in Java
2 parents 4a55426 + ab2ee36 commit 2af8ce7

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.gc;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* A simple Java program to demonstrate how to enable verbose Garbage Collection (GC) logging.
8+
* <p>
9+
* This simple program loads 3 million {@link java.lang.String} instances into a {@link java.util.HashMap}
10+
* object before making an explicit call to the garbage collector using <code>System.gc()</code>.
11+
* <p>
12+
* Finally, it removes 2 million of the {@link java.lang.String} instances from the {@link java.util.HashMap}.
13+
* We also explicitly use <code>System.out.println</code> to make interpreting the output easier.
14+
* <p>
15+
* Run this program with the following arguments to see verbose GC logging in its complete form:
16+
* <pre>
17+
* -XX:+UseSerialGC -Xms1024m -Xmx1024m -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:/path/to/file/gc.log
18+
* </pre>
19+
* Where:
20+
* <ul>
21+
* <li><code>-XX:+UseSerialGC</code> - specify the serial garbage collector.</li>
22+
* <li><code>-Xms1024m</code> - specify the minimal heap size.</li>
23+
* <li><code>-Xmx1024m</code> - specify the maximal heap size.</li>
24+
* <li><code>-verbose:gc</code> - activate the logging of garbage collection information in its simplest form.</li>
25+
* <li><code>-XX:+PrintGCDetails</code> - activate detailed information about garbage collection.</li>
26+
* <li><code>-XX:+PrintGCTimeStamps</code> - include a timestamp in the output reflecting the real-time passed in seconds since the JVM started.</li>
27+
* <li><code>-XX:+PrintGCDateStamps</code> - include the absolute date and time at the start of each line.</li>
28+
* <li><code>-Xloggc</code> - by default the GC log is written to stdout. Specify an output file via this argument.</li>
29+
* </ul>
30+
* <p>
31+
* It should be noted that the first three arguments are not strictly necessary but for the purposes or the example
32+
* help really simplify things.
33+
*
34+
*/
35+
public class VerboseGarbageCollectorRunner {
36+
37+
private static Map<String, String> stringContainer = new HashMap<>();
38+
39+
public static void main(String[] args) {
40+
System.out.println("Start of program!");
41+
String stringWithPrefix = "stringWithPrefix";
42+
43+
// Load Java Heap with 3 M java.lang.String instances
44+
for (int i = 0; i < 3000000; i++) {
45+
String newString = stringWithPrefix + i;
46+
stringContainer.put(newString, newString);
47+
}
48+
System.out.println("MAP size: " + stringContainer.size());
49+
50+
// Explicit GC!
51+
System.gc();
52+
53+
// Remove 2 M out of 3 M
54+
for (int i = 0; i < 2000000; i++) {
55+
String newString = stringWithPrefix + i;
56+
stringContainer.remove(newString);
57+
}
58+
59+
System.out.println("MAP size: " + stringContainer.size());
60+
System.out.println("End of program!");
61+
}
62+
63+
}

0 commit comments

Comments
 (0)