These days I am reading a book about Java Performance. One of its chapter is about Parallel Garbage Collection, which I think is really short and key-points focused. In this post, I will summarize some points that I consider useful.
Parallel Garbage Collector is a stop-the-world parallel garbage collector, which stops all the application threads during garbage collection and performs the garbage collection work using multiple threads. As I said in the post ‘brief introduction to java generation memory‘, there are two memory generations in the JVM heap, which are the young generation and the tenured generation. The garbage collection in both of these two generations are stop-the-world and parallel.
However, one notable thing about the garbage collection in the tenured generation is that, besides memory reclaimation, Parallel Garbage Collector also performs java object compaction. This means Parallel Garbage Collector will move all the living java object together and place them at the beginning of the tenured generation. It will makes the promotion of the objects from the young generation to the tenured generation faster, because a contiguous free memory is available for object promotion.
The fast memory allocation for objects is called bump-the-pointer technique. Only the end position of the last allocated java object needs to be tracked. The object allocation in the tenured generation only checks whether the new object can fit into the memory between the end of the last allocated object and the end of the tenured generation. If yes, allocate the memory for the object and move the pointer to point to the end of the newly allocated java object.
Please note that memory consumption in the tenured generation usually occurs when objects in the young generation, which survives a threshold of young generation garbage collection, are moved or promoted from the young generation to the tenured generation.
If you have ever looked through the command line options for Parallel Garbage Collector, you may notice the two options named -XX:+UseParallelGC and -XX:+UseParallelOldGC. This is because when Parallel Garbage Collector was initially developed, it only used the parallel stop-the-world collector in the young generation. The garbage collection in the tenured generation still used the single-thread stop-the-world method. Parallel Garbage Collector was activated by the -XX:+UseParallelGC option.
However, as the time passes by, the request for adding multiple threads support during the tenured generation collection increases, so Parallel Garbage Collector later uses multiple threads to collect the tenured generation. This behavior is enabled by adding the -XX:+UseParallelOldGC option.
In fact, in the lastest JDK, e.g. JDK8, specifying -XX:+UseParallelGC also enables -XX:+UseParallelOldGC, and specifying -XX:+UseParallelOldGC also enables -XX:+UseParallelGC. You usually only need to use -XX:+UseParallelGC option.