JVM Memory Manager - Garbage Collector
This section describes what is a garbage collector and its role and responsibilities for managing the JVM memroy.
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:
- Must Be Safe - A garbage collector should never deallocate any live objects by mistakes.
- Must Be Comprehensive - A garbage collector should not leave any garbage uncollected for too long.
- Must Be Fast - A garbage collector may interrupt the execution of the application to perform garbage collection,
but those interruptions should be very short.
- Must Not Waste Space - A garbage collector should not waste too much free space because of fragmentation.
- Must Be Scalable - A garbage collector must be scalable on multiprocessor systems.
Last update: 2014.
Table of Contents
About This Book
Downloading and Installing JDK 1.8.0 on Windows
Downloading and Installing JDK 1.7.0 on Windows
java.lang.Runtime Class - The JVM Instance
java.lang.System Class - The Operating System
ClassLoader Class - Class Loaders
Class Class - Class Reflections
Sun's JVM - Java HotSpot VM
JRockit JVM 28.2.7 by Oracle Corporation
JVM Runtime Data Areas
►Memory Management and Garbage Collectors
Memory Management General Rules
Java Exception: "java.lang.OutOfMemoryError: Java heap space"
OutOfMemoryError Comparison of HotSpot and JRockit
Garbage Collection Demonstration
►JVM Memory Manager - Garbage Collector
Generational Garbage Collection in HotSpot
Young Generation Collection - Minor Collection
Tenured Generation Collection - Full Collection
HotSpot Default Garbage Collector - Serial Collector
"-XX:+PrintGCDetails" - Garbage Collection Logging
GC Log Messages on GarbageCollection.java
Serial, Parallel, Concurrent, and Regionalized Collectors
Parallel Collector GC Log Message Format
Parallel Compacting Collector GC Log Message Format
Concurrent Mark-Sweep Collector GC Log Message Format
Garbage First GC Log Message Format
Garbage Collection Tests
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
CDS (Class Data Sharing)
Micro Benchmark Runner and JVM Options
Micro Benchmark Tests on "int" Operations
Micro Benchmark Tests on "long" Operations
Micro Benchmark Tests in JIT Compilation Mode
Micro Benchmark Tests on "float" and "double" Operations
PDF Printing Version