diff --git a/core-java/src/main/java/com/baeldung/gc/VerboseGarbageCollectorRunner.java b/core-java/src/main/java/com/baeldung/gc/VerboseGarbageCollectorRunner.java new file mode 100644 index 0000000000..f57580bbb5 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/gc/VerboseGarbageCollectorRunner.java @@ -0,0 +1,63 @@ +package com.baeldung.gc; + +import java.util.HashMap; +import java.util.Map; + +/** + * A simple Java program to demonstrate how to enable verbose Garbage Collection (GC) logging. + *
+ * This simple program loads 3 million {@link java.lang.String} instances into a {@link java.util.HashMap}
+ * object before making an explicit call to the garbage collector using System.gc().
+ *
+ * Finally, it removes 2 million of the {@link java.lang.String} instances from the {@link java.util.HashMap}.
+ * We also explicitly use System.out.println to make interpreting the output easier.
+ *
+ * Run this program with the following arguments to see verbose GC logging in its complete form: + *
+ * -XX:+UseSerialGC -Xms1024m -Xmx1024m -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:/path/to/file/gc.log + *+ * Where: + *
-XX:+UseSerialGC - specify the serial garbage collector.-Xms1024m - specify the minimal heap size.-Xmx1024m - specify the maximal heap size.-verbose:gc - activate the logging of garbage collection information in its simplest form.-XX:+PrintGCDetails - activate detailed information about garbage collection.-XX:+PrintGCTimeStamps - include a timestamp in the output reflecting the real-time passed in seconds since the JVM started.-XX:+PrintGCDateStamps - include the absolute date and time at the start of each line.-Xloggc - by default the GC log is written to stdout. Specify an output file via this argument.
+ * It should be noted that the first three arguments are not strictly necessary but for the purposes or the example
+ * help really simplify things.
+ *
+ */
+public class VerboseGarbageCollectorRunner {
+
+ private static Map