Busy Java Developer's Guide to Memory Management

ted.neward@newardassociates.com | Blog: http://blogs.newardassociates.com | Github: tedneward | LinkedIn: tedneward

Objectives

What are we doing here?

Caveats

Before we get started....

Memory Management: Overview

How do we parcel out a finite resource?

Memory Management: Overview

Memory is a finite resource

Memory Management: Overview

Two fundamental means of management

Memory Management: Overview

Neither approach is perfect

Memory Management: Concepts

Getting some fundamentals down

Memory Management: Concepts

A memory management system has two tasks:

If we're lucky, it'll have a way to determine which space is which

Memory Management: Concepts

Qualities

Memory Management: Concepts

Qualities

Memory Management: Concepts

Concerns

Memory Management: Concepts

Glossary: Nouns

Memory Management: Concepts

Glossary: Nouns

Memory Management: Concepts

Glossary: Nouns

Allocation

Back to basics

Allocation

A process is just a giant empty space

Allocation

A process is just a giant empty space

Allocation

Technically, three kinds of storage

Core questions:

Allocation

Static storage

Allocation

Static questions:

Allocation

Stack storage (mechanics)

Allocation

Stack storage (implications)

Allocation

Stack questions:

Allocation

Heap storage

Allocation

Heap questions:

Allocation

Modern languages often use all three forms

Allocation algorithms

How do we give memory away?

Allocation algorithms

Sequential allocation

Allocation algorithms

Free-list allocation

Allocation algorithms

First-fit allocation

Allocation algorithms

Best-fit allocation

Reclamation

aka 'garbage collection'

Reclamation

All reclamation algorithms have several tasks:

Reclamation

Four basic types

Reclamation

Variations

"Second-order"/"hybrid"

Reclamation

Real-world GC implementations base off of these

Reference Counting

Objects track their own lifecycle

Reference Counting

Each object allocated carries with it a "reference count"

Reference Counting

History

Reference Counting

Questions of note:

Reference Counting

Consequences:

Reference Counting

Consequences:

Mark-Sweep GC

The garbage collection two-step

Mark-Sweep GC

Mark-Sweep collectors are two-phase collectors

Mark-Sweep GC

Mark Phase: Identifying objects still in use

Mark-Sweep GC

Sweep Phase: Reclaim unmarked objects

Mark-Sweep GC

Questions of note:

Mark-Sweep GC

Consequences:

Mark-Compact GC

Don't just sweep it, compact it down!

Mark-Compact GC

Mark-Compact collectors are two-phase collectors

Mark-Compact GC

Mark Phase: Identifying objects still in use

Mark-Compact GC

Compact phase: Rearranging the heap

Mark-Compact GC

Questions of note:

Mark-Compact GC

Consequences (identical to Mark-Sweep):

Mark-Compact GC

Consequences:

Copying Collectors

Allocating/reclaiming in large chunks

Copying Collectors

Copying collectors are also two-phase collectors

Copying Collectors

Setup

Copying Collectors

Reclamation

Copying Collectors

Questions of note:

Copying Collectors

Consequences

Generational Collectors

Separating the young and the old

Generational Collectors

Generational collectors build off of some core assumptions

Generational Collectors

Generational collectors partition the heap into parts

Generational Collectors

Pros/cons of generational collectors

Finalization

Giving reclaimed objects a chance to clean up

Finalization

Reclamation often requires cleanup

Finalization

Destructors

Finalization

Object finalizers

Finalization

Finalizers have costs

Finalization

Open finalizer questions

JVM Memory Management Overview

Because true love means never having to say 'delete'

JVM Memory Management Overview

Big Picture

JVM Memory Management Overview

Reachability analysis used to identify in-use objects

JVM Memory Management Overview

Control

JVM GCs: A History

Back in the beginning....

JVM GCs: A History

Java 1.0

JVM GCs: A History

JDK 1.1

JVM GCs: A History

JDK 1.3

JVM GCs: A History

Java 1.4, 5, 6, ...

JVM GCs: A History

Java8

java.lang.Object finalizers

Releasing non-memory resources

java.lang.Object finalizers

Additional cleanup

java.lang.Object finalizers

Finalizers

java.lang.Object finalizers

Finalizers are dangerous

java.lang.Object finalizers

... which is why they're deprecated

The Cleaner API

The new-and-improved way to finalize

The Cleaner API

Motivation

The Cleaner API

Better way to do it

The Cleaner API

java.lang.ref.Cleaner

The Cleaner API

Simplest Cleaner usage

import java.lang.ref.Cleaner;
    
public class SimpleCleaner {
    private static final Cleaner CLEANER = Cleaner.create();
    public SimpleCleaner() {
        CLEANER.register(this, () -> System.out.println("I'm doing some clean up"));
    }
    
    public static void main(String... args) {
        SimpleCleaner app = new SimpleCleaner();
        app = null;
    
        System.gc(); // On most systems, this should trigger cleanup.
    }
}

The Cleaner API

Care and concerns

JVM Tuning Advice

Where to start

JVM Tuning Advice

General JVM Tuning Advice

JVM Tuning Advice

General JVM Tuning Advice

JVM Tuning Advice

General JVM Tuning Advice

OpenJDK Memory Management

What comes out of the box

OpenJDK Memory Management

Implementation

OpenJDK Memory Management

GC Algorithms offered

OpenJDK Memory Management

OpenJDK/Oracle-specific stuff:

OpenJDK JVM GC Defaults

What are we working with?

OpenJDK JVM GC Defaults

Defaults on JVM start

OpenJDK JVM GC Defaults

Behavior-based Tuning

OpenJDK JVM GC Defaults

Maximum Pause Time

OpenJDK JVM GC Defaults

Throughput

OpenJDK JVM GC Defaults

Footprint

Epsilon (JEP-318) GC

The 'NOP' garbage collector

Epsilon (JEP-318) GC

Available since Java11

Epsilon (JEP-318) GC

Usage

Epsilon (JEP-318) GC

Why....?

OpenJDK: Serial Collector

All the work on one thread

OpenJDK: Serial Collector

Overview

OpenJDK: Serial Collector

Implications

OpenJDK: Parallel Collector

Spread the work across threads... for speed

OpenJDK: Parallel Collector

Overview

OpenJDK: Parallel Collector

Some tuning parameters

OpenJDK: Z Garbage Collector

Low-latency collector

OpenJDK: Z Garbage Collector

Overview

Garbage-First (G1) Collector

A mostly-concurrent collector

Garbage-First (G1) Collector

Overview

Summary

JVM Memory Management is...

Garbage Collection: Resources

Where to go to get more

Garbage Collection: Resources

Books

Websites

Garbage Collection: Resources

Classic papers

JVM Memory Management Resources

Some good stuff to know or get

JVM Memory Management Resources

Tools/Projects

Credentials

Who is this guy?

Appendices

More info on the subjects at hand