Garbage Collection in the HotSpot JVM 1.8

HotSpot Java Virtual Machine (JVM) only

Focus on version 1.8

Motivation & Goal

Manage object life-cycle automatically

Free as much memory as fast as possible

Main Job

Scan for unused objects

Remove them from memory

Compact the memory in use

Preliminary Concepts

Heap is the part of the memory processed by the garbage collector

Stop-the-world pauses freeze the application from a user's perspective

Many objects are short-lived (eg. created in a loop)

Client and Server class

JVM automatically classifies machine as client or server

May be overridden with command line flags

32-Bit JVM with single core or on Windows leads to client

Everything else, esp. all 64-Bit JVMs, lead to server

Generational Heap

All garbage collectors split the heap into generations

Old generation and young, further divided

Eden space where new objects are created

And two survivor spaces used alternating

Objects are basically promoted from one generation to next

Generational Heap - Benefits

Allows different algorithms for young and old generation

Faster to scan young generation

Shorter stop-the-world pauses

Algorithms

Serial Garbage Collector - Single-threaded

Throughput Collector - Multi-threaded

Concurrent Mark and Sweep (CMS) Collector - Minimizes pauses

Garbage First (G1) Collector - Minimizes pauses with large heaps

Serial Garbage Collector

Default for client class machines

Single-threaded collection of new and old generation

Stop-the-world pauses for both collections

Compacts the old generation on collection

Througput Collector

Default for server class machines

Collects multi-threaded

  • Two operations:
  • Minor collection
  • Full collection

Throughput - Minor Garbage Collection

Occurs when eden space occupancy exceeds threshold

Promotes everything out of eden space

Most of the objects are garbage and can be removed

Some are promoted to the survivor space

Other survivor space is cleared as well, some promotions to old

Throughput - Major Garbage Collection

Occurs when old generation occupancy exceeds threshold

Cleans or promotes everything out of young generation

Cleans everything out of old generation without active references

Concurrent Mark and Sweep Collector

Tries to eliminate long pauses during full GC

  • Three operations:
  • Young collection - similar to Throughput
  • Full collection - similar to Throughput
  • Concurrent cycle

CMS - Concurrent Cycle

JVM starts a concurrent cycle when heap sufficiently full

Consists of multiple phases, two of them are stop-the-world

Old generation is not compacted

Garbage First (G1) Collector

Minimizes pauses for large heaps (>4GiB)

Divides heap into regions but still generational

  • Four operations:
  • Young collection
  • Background, concurrent cycle
  • Mixed collection
  • Full collection

G1 - Young Collection

G1 young collection triggered when eden regions fill up

After collection there are no regions assigned to eden

and at least one region assigned to survivor

G1 - Background Marking Cycle

Several phases, some of them stop-the-world

At least one young collection during marking cycle

Marks old regions containing mostly garbage

G1 - Mixed Collection

Uses input of marking cycle

Combine young collection with collection of marked regions

Non-garbage of marked regions moved to other regions

Summary

Throughput collector (and basically Serial too) maximize throughput

Concurrent collectors (CMS & G1) minimize pauses