Garbage Collection : GC#2
Many types of GC collectors in Java. How to choose the right one.
Serial : Simplest, designed for small apps and on single thread.GC events are conducted serially in 1 thread. Stop the world approach. So it is not used in real time.
It is the default since JDK 7
Concurrent Mark Sweep :
Concurrent low pause collector, multiple threads are used for minor garbage collection.
Major GC is multi threaded and runs concurrently alongside application. Use more CPUS.
If you can allocate ore CPU, the CMS GC is a preferre3d chice for better performance.
Garbage Firs (G1):
Replacement for CMS
Designed for multi-threaded apps that have large heap size available (>4 GB)
Does not have separate regions for young and old generations.
Partition the heap in to a set of equal size regions and use multiple[le threads to scan them.
After mark phase is over, G1 knows which regions contains the most garbage objects and performs GC on those regions first.
Experimental Garbage Collectors.
Shenandoah: Release as part of JDK 12. GC cycle works currently with application threads. Can relocate objects concurrently. More CPU intensive.
ZGC : Release as part of JDK 11. Low latency, scalability and ease of use. Allows to application to continue running while it performs all garbage collection operations.
How to choose :
Factors : Most of the default setting will work.
Serial is fine for small data setup (>=100 MB) and or it will be run on a sigle processor with no-pause time requirements.
Parallel is preferable for peak performance for apps and with no-pause time requirements or pauses for 1 sec or longer are acceptable.
CMS/G1 is preferable is response time is important than overall throughput and garbage collection pauses must be kept shorter than approximately 1 sec.
Advantages of GC :
Frees the heap mem to accommodate new objects.
Options for tuning the GC to improve its efficiency.
Best Practice:
It is possible to manually run the GC with System.gc() or Rintime.gc() methods.
If u don't have enough memory to run your application, you will experience slowdowns, hangs, long collection times, :stop the world" events, and eventually out of memory errors. You can take the help of monitoring tool like jstat or Java Flight recorder to see if the heap usage grows indefinitely, which might indicate a bug in your code.
If small, default settings will work.
Best approach to tuning JAVA garbage collection is settings flags on the JVM.
No comments:
Post a Comment