Garbage Collection in Java –
In java, to create an object we need to use new keyword, but for deleting that object we don’t have any delete keyword, this is just because we do not need to delete that object explicitly it is automatically deleted by the garbage collector thread which is resides in JVM, and the process of deleting the unused or unreferenced object is called garbage collection in java. So how this garbage collector works and what exactly the garbage collection process? Let’s explorer.
Definition of Java Garbage Collection –
Java garbage collection is the process by which Java programs perform automatic memory management means automatically deletes already used or unused or unreferenced object . When our source code means java programs compiled by the compiler the bytecode gets generated and that can be run on JVM(Java Virtual MAchine). When Java programs run, objects are created inside a heap memory , which is a portion of memory where the data of our program gets stored. Sometimes, some objects will no longer be needed. The garbage collector finds these unused objects and deletes them from memory to free up it.
Memory allocation to the Object –
When a programmer runs the program, the JVM checks the size of an object. It differentiate between small and large objects. The small and large size of object depends on the version of JVM, the size of heap memory, the strategy of garbage collection, and the platform which was used to create that object. The size of an object is usually in between 2 to 128 KB.
The small size l objects are stored in Thread Local Area (TLA) which is a free space of the heap memory. When TLA gets full, it requests for new TLA to the JVM.
On the other hand, large size objects that do not fit inside the TLA are directly allocated into the heap memory.
When an object is eligible for Garbage Collection?
An object is eligible for garbage collection only if it is not used by any program or thread or that object is null. If two objects having reference (cyclic reference) of each other means first object reference is pointed to second object and vice-versa, and they do not have any live reference then both objects are collected by the garbage collector. The object is eligible for garbage collection if it is created inside any block and the reference goes out of the scope once the control gets out from that block.
How does the Java Garbage Collector Works?
There are multiple garbage collectors in JVM that are used for various use cases, all its garbage collectors follow the same basic process. In the first step of garbage collection, the unreferenced or unused objects are identified and marked as ready for garbage collection. In the second step, that marked objects are deleted. After that memory size gets compacted or shrink, so remaining objects are placed in a contiguous block at the start of the heap. The compaction process makes garbage collectors to easily allocate memory to new objects sequentially after the block of memory allocated to existing objects.
The heap is divided into three different sections:
1.Young Generation: The Newly created objects start from the Young Generation. The Young Generation is further subdivided into an Eden space and two Survivor spaces, Eden space is a space where all new objects start, and two Survivor spaces, where objects are moved from Eden space after completing one garbage collection cycle. When objects are garbage collected from the Young Generation, that could be a minor garbage collection event that happens in the heap memory.
2.Old Generation: Objects that are long-lived are moved from the Young Generation to the Old Generation. When objects are garbage collected from the Young Generation, that could be a major garbage collection event that happens in the heap memory.
3.Permanent Generation: The classes and methods of our program are stored in the Permanent Generation. Classes and methods that are no longer in use may be garbage collected from the Permanent Generation.
During the full garbage collection process, unused or unreferenced objects in all generations are garbage collected and heap memory gets free to allocate new objects.
Types of Garbage Collectors in java –
1.Serial Garbage collector –
Serial Garbage collector works in single-threaded environments means it uses the only one thread to perform garbage collection. It works by holding all the other threads of an application and executes only one thread that performs garbage collection activity. It means that threads of the java application is freeze by the serial garbage collector during the process of garbage collection and this process is known as stop the world event. Try to avoid the use of serial GC in the server environment. We can use it for simple java programs. If you want to use the serial garbage collector in your program, execute the -XX:+UseSerialGC JVM argument to activate it.
2.Parallel Garbage Collector –
Parallel Garbage Collector is the default Garbage Collector that JVM executes to perform Garbage Collection. The working of the parallel garbage collector is the same as the serial garbage collector,the only difference between both is serial and parallel garbage collector is that serial garbage collector uses a single thread for garbage collection process while the parallel garbage collector uses multiple threads for the garbage collection. Parallel Garbage Collector can use multiple CPUs to speed up the application throughput. So, it is also known as throughput collector. It is used if we want to execute a long process. If you want to use the parallel garbage collector, execute the -XX:+UseParallelGC JVM argument to activate it.
3.Concurrent Mark and Sweep (CMS) Garbage Collector –
CMS uses multiple threads that scan the heap memory and during the scanning process,, it marks the objects for removal , after scanning, it sweeps the marked objects. It does not freeze the application’s threads during the garbage collection process. GC threads simultaneously execute with the application’s threads. For this reason, it uses more CPU as compared to other GC.To use a CMS garbage collector, execute the -XX:+USeParNewGC JVM argument to activate it.
4.Garbage First (G1) Garbage Collector –
The Garbage First (G1) Garbage Collector is used if we have a large (more than 4GB) space in heap memory. It divides the heap memory into equal-sized (usually 1MB to 32MB) parts, prioritizes them, and then performs the parallel garbage collection on that parts based on the priority.
The Eden, survivors, and old areas use this equal-sized parts for the memory allocation of the objects.
SevenMentor Pvt Ltd
Call the Trainer and Book your free demo Class for now!!!
© Copyright 202! | Sevenmentor Pvt Ltd.