Memory Management and Garbage Collectors
This chapter provides tutorial notes and example codes on memory management and garbage collectors. Topics include JVM memory management guidelines; OutOfMemoryError exception; Garbage Collection (GC); Young and Tenured Heap regions; Eden and Survivor spaces; Mark-Sweep-Compact algorithm; serial, parallel, concurrent and regionalized collectors; GC details logging.
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
- JVM raises OutOfMemoryError exception when the Java application requires more memory
than its given maximum limit.
- OutOfMemoryError exceptions happen mostly in the Heap data area.
- The Heap area memory management is done by the garbage collector, which allocates and deallocates data objects automatically for the Java application.
's garbage collector automatically collects dead objects to free up used memory.
- HotSpot JVM Heap area is divided into 2 generations: Young Generation and Tenured (Old) Generation.
- The Young Generation is divided into 3 areas: Eden, Survivor Space 'From' and Survivor Space 'To'.
- When the Eden area is full, a Minor Garbage Collection (GC) is performed, which moves live objects
from the Eden area to the Survivor Space 'To' area. It also empties the Survivor Space 'From' area
by moving "young" live objects to the Survivor Space 'To' area and promoting "old" live objects to the Tenured Generation.
- When the Tenured Generation is full, a Full Garbage Collection (GC) is performed, which
follows a 3-step, Mark-Sweep-Compact (MSC), process to deallocate dead objects and defragment free space.
- The Serial Collector, -XX:+UseSerialGC, performs both Minor and Full GC in a stop-the-world fashion and serially using 1 CPU only.
- The Parallel Collector, -XX:+UseParallelGC, improves the performance of the Serial Collector by performing Minor GC parallelly using multiple CPUs.
- The Parallel Compacting Collector, -XX:+UseParallelOldGC, improves the performance of the Parallel Collector by performing Full GC parallelly using multiple CPUs.
- The Concurrent Mark-Sweep (CMS) Collector, -XX:+UseConcMarkSweepGC, improves the performance of the Parallel Compacting Collector
by performing Full GC concurrently while the Java application is running.
- The Garbage First (G1) Collector, -XX:+UseG1GC, improves the performance of the Concurrent Mark-Sweep (CMS) Collector
by dividing the Heap area into regions and working on regions mostly filled with garbage first.
- The -XX:+PrintGCDetails option generates GC detail log messages helping us to know how well the garbage collector is doing its job.
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
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