Usage
The option “-XX:+PrintGC” turns on basic Garbage Collector logging.
Since:
Starting from JDK 6.
Deprecated:
Deprecated, use “-Xlog:gc” instead.
Syntax:
java -XX:+PrintGC MainClass
Examples:
To turn on the logs for Garbage Collector: java -XX:+PrintGC MainClass
Description:
Before talking about the flag, we need to introduce Garbage Collector (GC), Young and Old Generations:
Garbage Collector is a way of the JVM to clean the memory and makes the java memory more efficient. It tracks (Mark step) and removes (sweep step) every unused object available in the JVM heap space. An object is considered unused when it’s referenced by null, assigned to another reference or by anonymous object …. There are 5 type of GC implementations:
- Serial Garbage Collector
- Parallel Garbage Collector
- CMS Garbage Collector
- G1 Garbage Collector
- Z Garbage Collector
Young Generation is a place where all new objects start out and aged. It’s divided into two spaces:
- Eden Space: New object initiated with the keyword “new” are allocated in this memory space.
- Survivor Space: When the Eden space is filled. A minor garbage collection event occurs. Objects that are referenced are marked and some are not. The ones which are marked will move to Survivor Space S0 and the one unmarked are removed by GC. When The space S0 filled, the cycle repeat itself but instead of moving them to S0, they will be moved to Survivor Space S1.
Old Generation is a place where objects that lived for a long time lie. If an object reaches an age threshold after surviving multiple GC events in the Young Generation, the object gets moved to the Old Generation.
This flag comes in handy when you want to diagnose any memory problems. It is an alias for “-verbose:gc” and turns on basic Garbage Collector (GC) logging. In this mode, a single line is printed for every young-generation and every full-generation collection.
An example of output when using java -X:+PrintGC MainClass
0.082: [GC (Allocation Failure) 1750K->1035K(3045K), 0.0006865 secs]
1.081: [GC (Allocation Failure) 3564K->3246K(5650K), 0.0016581 secs]
1.090: [Full GC (Allocation Failure) 3246K->3246K(5650K), 0.0023716 secs]
0.082 – time from the start of the program
Full GC/GC - the type of GC that was running
1750K->1035K(3045K) - After GC cleaning the memory, the memory reduced from 1750k to 1035k and total memory is 3045k.
0.0006865 secs - the time that GC took to ran
Default Value:
This option is disabled by default.
Errors:
None.
Arguments related:
PrintGCDetails
Related Posts:
Edit your Comment