Monday, 10 June 2019

Java Garbage Collection

Garbage Collection :

1. Serial garbage collector
2. Parallel collector
3. Incremental low pass
4. Mark sweep collector 1.5 to 1.7
5. GC1 collector


- Java Provides automatic memory management through a program called Garbage Collector.
"Removes objects those are no longer used"

- Objects are created in the heap area for eg: MyObject obj =new MyObject();
here obj is the reference and with new keyword it will allocate area in the heap memory.

- Static members, class definitions are placed in method area i.e. the perm gen space

So when the object is not used anymore, then it is taken care by a daemon thread i.e. "Garbage Collector"

This process will run by Java and we can't force the thread to start.
System.gc()
Only we can request Java to run GC but here is the twist still there is not guarentee that this thread will run.

java.lang.OutOfMemoryError
This happens when there is no space left in heap i.e. now the heap is full and then program tries to create a new object.

Task: Search about heap generations i.e. Young Generation Heap, Old Generation Heap and Permanent Generation Heap space



- Serial collector :
Uses a single thread to perform all garbage collection work, which makes it relatively efficient because there is no communication overhead between threads. It is best-suited to single processor machines -XX:+UseSerialGC.

- Parallel collector:
(also known as the throughput collector) performs minor collections in parallel, which can significantly reduce garbage collection overhead. It is intended for applications with medium-sized to large-sized data sets that are run on multiprocessor or multithreaded hardware.

- Concurrent collector :
Performs most of its work concurrently (for example, while the application is still running) to keep garbage collection pauses short. It is designed for applications with medium-sized to large-sized data sets in which response time is more important than overall throughput because the techniques used to minimise pauses can reduce application performance.

- Concurrent Mark Sweep (CMS) :
Paired with ParNew, works really well for server-side applications processing live requests from clients. I have been using it with ~ 10GB of heap memory and it keeps response times steady and GC pauses are short. Some developers I know use Parallel collectors (Parallel Scavenge + Parallel Old) and are happy with results.
One important thing to know about the CMS is that this has been deprecated

- GC1 :
The G1 collector is a server-style garbage collector, targeted for multi-processor machines with large memories. It meets garbage collection (GC) pause time goals with high probability, while achieving high throughput.
G1 works on both old and young generation. It is optimized for larger heap sizes (>10 GB). I’ve not experienced G1 collector first-hand and developers in my team are still using CMS, so I can’t yet compare the two. A quick online search reveals benchmarks showing CMS outperforming G1. I’d tread carefully, but G1 should be fine. It can be enabled with:

-XX:+UseG1GC


Let's talk more about GC1

GC1 is short for Garbage First. It has been experimentally launched in the Java 1.6 update 14 to replace the regular Concurrent Mark and Sweep Garbage Collectors with increased performance.

G1 is considered as “server centric” with following attributes.

1) G1 uses parallelism which are mostly used in hardware today.The main advantage of G1 is designed in such a way to make use of all available CPU’s and utilize the processing power of all CPU’s and increase the performance and speed in Garbage Collection.
2) Concurrency feature of G1 allows to run Java threads to minimize the heap operations at stop pauses.
3) Next feature which plays a key role in increasing the Garbage Collection is treating the young objects(newly created) and the old objects(which lived for some time) differently.G1 mainly focuses on young objects as they can be reclaimable when traversing the old objects.
4) Heap compaction is done to eliminate fragmentation problems.
5) G1 can be more predictable when compared to CMS.

Features of G1 Garbage Collector:

1) A single contiguous heap which is split into same-sized regions. No separation between younger and older regions.
2) G1 uses evacuation pauses.Evacuation pauses are done in parallel by using all the available processors.
3) G1 uses a pause prediction model to meet user-defined pause time targets
4) Like CMS,G1 also periodically performs a concurrent marking phase.
5) Unlike CMS, G1 does not perform a concurrent sweeping phase


Happy Learning 😊

No comments:

Post a Comment