A Garbage Collector is a Java program which tracked the referenced (live) objects and allowed them to keep in the heap memory whereas the memory of the unreferenced (dead) objects is reclaimed and reused for future object allocation. This method of reclaiming unused memory is known as Garbage Collection. In Garbage collection, neither the explicit deletion of the memory happens nor the memory is given back to the operating system. As long as an object is being referenced, the JVM considers it alive and keeps it in the memory. Once the object is no longer referenced (dead) or not reachable by the application code then the garbage collector removes it and reclaims the unused memory. This process is also called automatic memory management.
Garbage collection is related to memory management and helps to improve the performance of the application. Garbage collection in a system should not be too quick or too late. Too many GC cycles degrade the performance of the system and cause a spike in CPU whereas a delay in GC cycles leads to memory leakage.
Example:
Let’s consider you are a bachelor and get a call from your mom that she is coming to your flat in the next 3 hours. While talking, you just turn around and see the condition of the flat which is worst than ever think. When the call gets over the first question comes to your mind “Where would I let my mom sit in the room?”. See the left side snap of the room with all the important and garbage things that are messed up and not a single place even to stand.
After taking a deep breath, you start picking the things and placing them in their proper place. Then start dusting and brooming the room. Also, trash the garbage in the bin and make the space available to walk and sit. After 3 hours of breakless effort, you made the room clean in which you can comfortably welcome your mom. Refer to the right-side picture of the same room.
This is a real-life example of a Garbage Collector where the room is a heap memory, important households are live/reachable objects which need to be placed in the proper place and garbages are the dead/unreachable objects which need to be thrown out so that room space can be reclaimed, your mom and her luggage are new objects for which you have created some spaces and at last, you were acting like a Garbage Collector.
Type of Garbage Collector:
There are four types of Garbage Collectors:
- Serial Garbage Collector
- Parallel Garbage Collector
- CMS Garbage Collector
- G1 Garbage Collector
Garbage Collection method involves:
- Mark: To mark the generation of the object.
- Delete/Sweep: To delete the unreachable/dead object.
- Compact: To compact the memory by moving around the object.
Some important terms:
- Live Object: The object which is referenced by another object
- Dead Object: The object which is not referenced by any other object and is unreachable
- Daemon Thread: Garbage collection process is carried out by Daemon Thread
- System.gc(): It is used to invoke the garbage collector and on invocation, the garbage collector will run to reclaim the unused memory space. The System.gc() is a static method.
You may be interested:
What could be interview question on this topic?
Hi Kapil,
We will share some interview question soon on GC topic.
please share !
Please share Performance Engg related interview questions