Last Updated: 28 May, 2022
Garbage collection is a technique by which Java performs memory management automatically. When Java programs run on the JVM, objects are created on the heap memory.
The Garbage Collector(GC) finds the unused objects (no longer referenced or not used by the Java program) and deletes them to reclaim the memory.
Garbage Collector is a program that runs by daemon thread in the background to manage the heap memory. It takes the responsibility for the de-allocation of objects.
The Garbage Collector checks the objects if there are no references to an object, it is assumed to not be in use and eligible for Garbage collection. If any object is eligible for garbage collection, then the memory occupied by the object can be reclaimed. The mechanism of reclaiming memory is known as Garbage Collection.
Heap Memory: The heap memory is the dedicated area of memory used to store objects instantiated by the Java programs running on the JVM. When the JVM is started, heap memory is created. When we create an object using the new operator it is always created inside heap memory. If heap memory is full, Java throws java.lang.OutOfMemoryError.
Java garbage collection is an automatic process. Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. An in-use object, or a referenced object, means that some part of your program still maintains a pointer to that object. An unused or unreferenced object is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed. The programmer does not need to mark objects to be deleted explicitly. The garbage collection implementation lives in the JVM.
There are five types of Garbage Collector:
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com