JVM GC
Garbage Collection #
Garbage Collection is one of Java's memory management methods, a process that periodically deletes memory areas that are no longer needed from the JVM Heap area after being dynamically allocated. Unlike C and C++, which lack such garbage collection and require programmers to manually allocate and deallocate memory, Java's JVM-embedded garbage collector handles memory management. This allows developers to focus solely on development without having to perfectly manage memory or memory leak issues, which is a significant advantage.
Disadvantages #
- Developers cannot precisely know when memory is deallocated.
- Garbage collection causes overhead because other operations are paused while it runs.
Objects Subject to Garbage Collection #

Objects are actually created in the Heap area, and Root Areas like the Method Area or Stack Area only reference the addresses of objects created in the Heap Area. However, when reference variables holding the memory addresses of these Heap Area objects are deleted due to specific events, such as a method ending, objects that are not referenced anywhere in the Heap area (like the red object in the image above) become present. These objects are called 'unreachable' and are periodically removed by the garbage collector.
Reachable: The state where an object is being referenced Unreachable: The state where an object is not being referenced
Mark And Sweep Algorithm #

The Mark And Sweep algorithm is the principle by which garbage collection operates, using the accessibility of an object from the root as the criterion for memory deallocation. Mark And Sweep is divided into three main stages, as shown in the figure above.
- Mark
- First, it traverses the graph from the roots to find connected objects, identifies which objects each references, and marks them.
- Sweep
- It removes unreferenced objects, i.e., Unreachable objects, from the Heap.
- Compact
- After the sweep, it moves fragmented objects to the beginning of the Heap, compacting the allocated and unallocated memory areas. (Some garbage collectors may not perform this step.)
Heap Area Subject to GC #

For efficient GC, the Heap Area is divided into Eden, Survivor, and Old Generation, as shown above.
GC Operation Process #
First Stage #

When an object is first created, it is allocated in the Eden space of the Heap with an age-bit of 0. This age-bit increments by 1 each time it survives a Minor GC.
Second Stage #

After some time, when the Eden space of the Heap Area is full of objects, a Minor GC occurs. Objects are then either moved to the Survivor space or reclaimed, depending on their reachability.
Third Stage #

New objects continue to be created in the Eden space. When the Eden space fills up again, objects in the Young Generation (Eden + Survivor) are moved to the empty Survivor space, Survivor1, and the age of all surviving objects increases by 1.
Fourth Stage #

When the Eden space is again filled with new objects, another Minor GC occurs. Objects in the Young Generation are moved to the empty Survivor space, Survivor0, and their age is incremented by 1. This process repeats continuously.
Fifth Stage #
![]()
As this process repeats, the age-bit may exceed a certain number. When it reaches the age-bit threshold configured in the JVM, the object is deemed long-lived and moved to the Old Generation space. This process is called promotion.
Final Stage #

Over time, if the memory allocated in the Old space exceeds its limit, a GC is executed that inspects all objects in the Old space and simultaneously deletes unreferenced objects. This GC, which reclaims memory in the Old Generation space, is called Major GC. Major GC is a time-consuming operation, and during its execution, all threads except the GC thread are paused. This is known as Stop-the-World. If this operation occurs too frequently, it can negatively impact program performance.