A garbage collector is a Java program that keeps track of referenced (alive) objects and allows them to remain in heap memory while reclaiming memory from unreferenced (dead) objects and reusing them. for further assignment of objects. Garbage collection is the term used to describe this technique for reclaiming unused memory. Memory is not explicitly removed during garbage collection or returned to the operating system. The JVM considers an object to be alive and keeps it in memory for as long as it is referenced. The garbage collector disposes of the object and frees memory when it is no longer referenced (dead) or cannot be accessed by application code. Another name for this procedure is automated memory management.
Garbage collection has implications for memory management and helps improve program performance. The collection of waste from a system should not be too early or too late. A delay in GC cycles causes a memory leak, while too many GC cycles reduce system performance and increase CPU usage.
Example:
Consider being single and receiving a call from her mother informing you that she will be at her apartment in three hours. You just turn around and look at the worse-than-expected condition of the apartment as you speak. The first thing you think of when you finish talking on the phone is, “Where would I put my mom in the room?” He takes a look to the left side of the room to see how all the items, both valuable and useless, are in disarray and there isn’t even a place to stand.
Example 01: Left – Before GC; Right – After GC
You start to pick up the items and put them where they belong after taking a deep breath. Then start sweeping and dusting the room. Also, please put trash in the trash and clear a place where people can walk and sit. You cleaned the room after three hours of non-stop work so that you could comfortably receive your mother. Please refer to the image on the right-hand side for the identical room.
This is a real-world example of a garbage collector where the room is a pile of memories, the household essentials are live/reachable objects that need to be placed in the correct position, and the trash is the dead/unreachable objects that need to be placed. drop out to free up space. Your mom’s luggage and other new items are now in the locations you created for them, and you finally functioned as a garbage collector.
Type of Garbage Collector:
There are four different categories of garbage collectors:
- Serial Garbage Collector
- Parallel Garbage Collector
- CMS Garbage Collector
- G1 Garbage Collector
The Garbage collection method includes:
- Mark: to indicate the generation of the object.
- Delete/Sweep: To delete the unreachable/dead object.
- Compact: To compact the memory by moving around the object.
Some crucial words:
- Living objects are objects used as a reference by other objects.
- Dead objects are inaccessible and not referenced by other objects.
- Daemon Thread: Daemon Thread manages the garbage collection procedure.
- 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.