Wednesday, 6 July 2011

Garbage Collection: Serial vs. Parallel vs. Concurrent-Mark-Sweep

By default, the JDK uses a serial garbage collector. When it runs, the garbage collector stops all application execution in order to do its job. This works really well for desktop-based, client applications which are running on one processor. For server-based, multi processor systems, you will perhaps want to switch to
the parallel garbage collector known as the Concurrent Mark-Sweep collector (CMS). This collector makes one short pause in application execution to mark objects directly reachable from the application code.
Then it allows the application to run while it marks all objects which are reachable from the set it marked.
Finally, it adds another phase called the remark phase which finalizes marking by revisiting any objects modified while the application was running. It then sweeps through and garbage collects.



what's the difference between the serial, parallel and CMS (Concurrent-Mark-Sweep) collectors?

first of all, let's take a look which collectors operate on the young generation and which on the tenured (old) generation:

  • the following collectors operate on the young generation:
-XX:+UseSerialGC
-XX:+UseParallelGC
-XX:+UseParNewGC

  • the following collectors operate on the old generation:
-XX:+UseParallelOldGC
-XX:+UseConcMarkSweepGC

  • what's the difference between the Serial and the Parallel collector?
both the serial and parallel collectors cause a stop-the-world during the GC.
so what's the difference between them?
a serial collector is a default copying collector which uses only one GC thread for the GC operation, while a parallel collector uses multiple GC threads for the GC operation.

  • what's the difference between the Parallel and the CMS collector?
the CMS performs the following steps (all made by only one GC thread):
- initial mark
- concurrent marking
- remark
- concurrent sweeping

there are two differences between a parallel and a CMS collectors:
1) the parallel uses multiple GC threads, while the CMS uses only one.
2) the parallel is a 'stop-the-world' collector, while the CMS stops the world only during the initial mark and remark phases.
during the concurrent marking and sweeping phases, the CMS thread runs along with the application's threads.

  • if you wish to combine both parallelism and concurrency in your GC, you can use the following:
-XX:UserParNewGC for the new generation (multiple GC threads)
-XX:+UseConcMarkSweepGC for the old generation (one GC thread, freezes the JVM only during the initial mark and remark phases)

1 comment: