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:

Last update: 2018.

Table of Contents

 About This Book

 Heap Memory Area and Size Control

 JVM Garbage Collection Logging

Introduction of Garbage Collectors

JVM Memory Manager - Garbage Collector

 Generational Garbage Collection in HotSpot

 Young Generation Collection - Minor GC

 Young Generation Collectors - Serial, PS, ParNew, G1

 Tenured Generation Collection - Major GC

 Tenured Generation Collectors - Serial, ParOldGen, CMS, G1

 Collector Combinations: Serial, Parallel, Concurrent, G1

 Serial Collector - "+XX:+UseSerialGC"

 Parallel Collector - "+XX:+UseParallelGC"

 Concurrent Mark-Sweep (CMS) Collector - "+XX:+UseConcMarkSweepGC"

 Garbage First (G1) Collector - "+XX:+UseG1GC"

 Object References and Garbage Collection

 Garbage Collection Performance Test Program

 Performance Tests on Serial Collector

 Performance Tests on Parallel collector

 Performance Tests on Concurrent collector

 Performance Tests on G1 collector

 Garbage Collection Performance Test Summary

 Outdated Tutorials

 References

 Full Version in PDF/EPUB