Java GC Tutorials - Herong's Tutorial Examples - v1.10, by Dr. Herong Yang
JVM Memory Manager - Garbage Collector
This section describes what is a garbage collector and its role and responsibilities for managing the JVM memory.
In previous sections, we have observed how JVM manages the Heap memory automatically for us. In following sections, we are going to learn some basic concepts on how the JVM actually manages the memory.
What Is a Garbage Collector? A garbage collector is JVM component that is responsible for managing memory space primarily in the Head data area. It also manages memory space in Method Area and Direct Memory data areas. More specifically, a garbage collector has 3 major responsibilities.
1. Allocating Memory for New Objects - When the Java application is requesting to create a new data object (an instance of a class or array), the garbage collector must find a block of free space of a certain size in the memory. It is an easy task at the beginning, when the free space is a single continuous chuck. But when the free space becomes fragmented because of old objects are deallocated not in the same order as them were allocated. The garbage collector may have to move data objects around to put free spaces together into large chunks.
2. Ensuring Live Objects Remaining in Memory - Once a data object is allocated, the garbage collector must keep tracking it to know where this object is actually located in the memory, and how many live references that are pointing this object.
3. Deallocating Memory for Dead Objects - When a data object loses its last live reference, it becomes a dead object, which is also called garbage. The garbage collector can leave dead objects in the memory for some time. But it must deallocate them when there is not enough free space to allocate new objects. The process of finding dead objects and freeing the space used by these objects is known as garbage collection.
Common design goals of a garbage collector:
Table of Contents