The JVM and Java Garbage Collection Notes

(This notes is from watching The JVM and Java Garbage Collection)

The JVM and Java Garbage Collection

Prerequisites

  • Some experience with Java SE development
  • Some experience with CLI

This talk uses Java JDK 7 u25, and Visual VM with the Visual GC Plugin.

JVM Overview

Two key components to Java: The programming language and JVM

  • Java language
  • Architecture of HotSpot VM
  • Key HotSpot VM components

Java language

  • OOPL that improves on C/C++
  • automatic GC
  • source code is compiled into byte code
  • byte code stored in .class files
  • .class files are loaded in JVM and executed
  • a separate JVM is created for each Java application

HotSpot JVM: Architecture

HotSpot JVM: Architecture

The talk will focus on heap, JIT compiler, and GC.

GC basics

  • GC is a way in which java recollects the space occupied by loitering objects
  • In general, GC is responsible for 3 tasks:
    • Allocating memory for new objects
    • Ensuring that any referenced objects remain in memory
    • Recoverying memory used by objects that are no longer reachable

Marking

Each object the GC meets is marked as being used, and will not be deleted in the sweeping stage.

Marking

Sweeping

  • The sweeping stage is where the deletion of objects take place
  • The traditional way is to let the allocator methods use complex data structures to search the memory for the required free space

Sweeping

Compacting

  • Compact the memory by moving objects to each other
  • Object allocation is faster

Compacting

Generational GC

Why Generational GC

  • Non-Generational GC will just iterate over every object in the heap:
    • Increased objects reduces performance
    • Ignores typical object behavior
  • Generational GC improves upon this

Generational GC

  • HotSpot uses “generational collectors”
  • HotSpot Java heap is allocated into generational spaces

Generational GC

Major Spaces

  • The memory spaces is divided into three sections:
    • Young generation
      • Eden
      • A “from” survivor space
      • A “to” survivor space
    • Tenured generation
    • Permanent generation (meta data, classes, and so on)

Generational GC

Features of Young Generation Space

  • GCs occur relatively frequently
  • Gcs are fast and efficient because young generation space is usually small and likely to contain a lot of short lived objects
  • Objects that survive some number of young generation collections are promoted to old generation heap space

Features of Old Generation Space

  • Typically larger than young generation heap space
  • Its occupancy grows more slowly
  • GCs are infrequent but takes significantly longer time to complete than younger generational heap space
    • GCs in old generation space should be minimized

Generational GC

Promotion

Young Generation

Young GC Process

  • New objects are allocated to the eden space
  • When eden space is full, a minor GC is triggered
    • STW event

Young GC 1

  • Unreferenced objects are GCed
  • Referenced objects are copied to survivor space and have their age incremented

Young GC 2

  • After objects are moved to the survivor space
    • Eden space is cleared
    • The “from” survivor space is also cleared

Young GC 2A

  • Next minor GC
    • Referenced objects from last GC become “from” survivor space
    • Referenced objects are copied to the “to” survivor space
    • Surviving object ages are incremented

Young GC 3

  • Next minor GC
    • Referenced objects from last GC become “from” survivor space
    • Referenced objects are copied to the “to” survivor space
    • Surviving object ages are incremented

Young GC 4

  • Promoted to Old Space
    • When age threshold is reached, objects are eventually promoted to tenured space

Young GC 5

  • Process repeats at each minor GC
  • When objects reach an age threshold, they are copied to old generation

Young GC 6

Young Generation Recap

  • Young generation
    • Eden
    • Survivor spaces
    • Objects age here
  • Minor GC are always STW events
  • Minor GC can be
    • Single-threaded
      • Serial GC
    • Multi-threaded
      • Parallel GC
      • Concurrent GC
      • G1 GC

Old and Permanent Generations

  • Old Generation
    • Contains objects that have reached the minor collection age threshold
    • Major GC - Time and resource intensive
  • Permanent Generation
    • Contains metadata required by the JVM
    • Class objects and methods
    • Interned Strings

Generational GC

Demo

Using Java VisualVM to visualize VM usage.

G1 GC

  • Garbage First GC
    • Server-style GC targeted for multi-processor machines with large memories
    • Meets GC pause time goals with a high probability, achieving high throughput
  • The G1 collector is designed for applications that
    • Can operate concurrently with application threads like the CMS collector
    • Compact free space without lengthy GC induced pause times
    • Need more predictable GC pause durations
    • Do not want to sacrifice a lot of throughput performance
    • Do not require a much larger Java heap

Young Generation Overview

  • The heap is a single memory space split into regions
    • Young generation memory is composed of a set of non-contiguous regions
    • This makes it easy to resize when needed
  • Young generation GC are STW events
  • The young GC is done in parallel using multiple threads
  • Live objects are copied to new survior or old generation regions

G1 Heap Structure

One memory area split into many fixed sized regions:

G1 Heap Allocation

Young Generation in G1

G1 Heap Allocation 1

G1 Heap Allocation 2

G1 Heap Allocation 3

G1 Concurrent Marking Phases

  • (1) Initial mark (STW event)
  • (2) Root Region Scanning
  • (3) Concurrent Marking
  • (4) Remark (STW event)
  • (5) Cleanup (STW event and concurrent)
  • (*) Copying (STW event)

Initial Marking Phase

Initial Marking Phase

Concurrent Marking Phase

Concurrent Marking Phase

Remark Phase

Remark Phase

Copying Cleanup Phase

Copying Cleanup Phase

After Copying/Cleanup Phase

After Copying Cleanup Phase

Demo

(Again) Using Java VisualVM to visualize VM usage.