Wednesday 5 June 2019

Different Garbage Collectors


Java support below garbage collectors.
a.   Serial Generational Collector (-XX:+UseSerialGC)
b.   Parallel Collector (-XX:+UseParallelGC)
c.    Concurrent Mark and Sweep collector (-XX:+UseConcMarkSweepGC)
d.   G1 Garbage collector (-XX:+UseG1GC)

you can choose which garbage collectors to use to your application. For example, by passing the flag ‘-XX:+UseSerialGC’ to JVM, you are asking JVM to use serial generational collector to perform garbage collection.

Serial Generation Collector
Serial collector is single threaded, it pauses all the application threads while performing garbage collection. To turn on this collector use the option ‘-XX:+UseSerialGC’ while launching the application. This collector is mainly designed for single threaded environment, not suitable for server side applications.

This collector uses Mark and Sweep algorithm internally.

Parallel collector
This use parallel threads for young and old generation garbage collection. The parallel collector is enabled with the command-line option -XX:+UseParallelGC.

You can control the number of garbage collector threads with the command-line option -XX:ParallelGCThreads=N. N represent number of threads to use for parallel garbage collection.

For more information, refer below post.

Concurrent Mark and Sweep collector (CMS)
This uses mark and sweep algorithm to perform garbage collection. It pauses all the application thread while performing garbage collection. This is preferred for any application with a low pause time requirement.

For more information refer below post.

G1 Garbage Collector
G1 stands for Garbage first collector. Previous three garbage collectors (serial, parallel, CMS) structures the heap into 3 sections.
a.   Young Generation
b.   Old Generation
c.    Permanent Generation

G1 collector addresses this problem in different perspective. Entire heap is partitioned into set of equal sized regions. Some regions are given to Eden space, some are given to survival space and some are given to old generation.



G1 performs a concurrent global marking phase to determine the liveness of objects throughout the heap. After the mark phase completes, G1 knows which regions are mostly empty. It collects in these regions first, which usually yields a large amount of free space. G1 concentrates its collection and compaction activity on the areas of the heap that are likely to be full of reclaimable objects, that is, garbage. G1 uses a pause prediction model to meet a user-defined pause time target and selects the number of regions to collect based on the specified pause time target.

How to choose the garbage collector?
Profile the application by simulating production load and apply these garbge collectors one by one and choose which one fits for you.

Reference


Previous                                                 Next                                                 Home

No comments:

Post a Comment