106
GC, University of Tokyo, Sep 22, 2003 1/92 Garbage Collection: Overview, Techniques, Successes Tony Printezis [email protected] Sun Microsystems Laboratories MS BUR02-311 1 Network Drive Burlington, MA 01803 USA

Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 1/92

Garbage Collection:Overview, Techniques, Successes

Tony Printeziston [email protected]

Sun Micr osystems LaboratoriesMS BUR02-3111 Network Drive

Burlington, MA 01803USA

Page 2: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 2/92

Who Am I?

■ Tony Printezis● Member Of Technical Staff, JTech Group,● Sun Microsystems Laboratories, East, MA

■ Previously● Faculty Member, Dept of Computing Science,● University of Glasgow, Scotland

■ Working on GC for 5 years● wrote first version of mostly-concurrent GC

Page 3: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 3/92

Overview

■ Introduction / GC Benefits■ Simple GC Techniques■ Incremental GC Techniques■ Generational GC Techniques■ GC in the Java HotSpot Virtual Machine■ GC Issues in the Real World■ Conclusions

Page 4: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 4/92

Garbage Collection

■ Traditional Explicit De-Allocation (C/C++)● Programmer allocates memory (new/malloc)● Programmer also has to de-allocate it

(delete/free)■ Automatic Memory Management (aka GC)

● Programmer allocates memory (new)● GC reclaims all unused memory

Page 5: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 5/92

GC Brief Histor y

■ GC has existed since the 1960s!● LISP

■ Functional / O-O / Logic languages● ML, Haskell, SmallTalk, etc.

■ Conservative GCs● C and C++

■ Mainstream (finally!) in the late 1990s● the Java programming language, then C#

Page 6: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 6/92

GC Benefits

✔ No dangling references● (wrongly de-allocated memory)

✔ No memor y leaks● (unused, not de-allocated memory)

✔ Greater programmer productivity● no need to de-allocate memory● simplified team work● simplified APIs

Page 7: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 7/92

The Java Langua ge Benefits GC Too

■ “Chicken and Egg” problem● no good GC no applications use it● no applications to test no improved GC

■ The Java language is great for GC research● large amounts of industrial-strength code● several industrial-strength JVMs● industry-standard benchmarks● academia / industry both interested

Page 8: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 8/92

Programmer s and GC

Three categories of programmers:

✔ Learned to program using garbage collection;really hate explicit de-allocation

✔ Learned to program using explicit de-allocation;migrated smoothly to garbage collection (me!)

✘ Learned to program using explicit de-allocation;really hate garbage collection

Page 9: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 9/92

Memor y Costs and GC

■ Why do we need GC anyway?● memory these days is really cheap ( $200/GB)● 64-bit address space is really huge● can’t we keep adding more memory?

■ One “real-world” application allocates● 20MB/sec, 1.2GB/min, 70.3GB/hour● translates to $14,000/hour (quite expensive!)● but, it would still take 27,000 years to fill up

the 64-bit address space though!

Page 10: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 10/92

Overview

■ Introduction / GC Benefits

➜ Simple GC Techniques■ Incremental GC Techniques■ Generational GC Techniques■ GC in the Java HotSpot Virtual Machine■ GC Issues in the Real World■ Conclusions

Page 11: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 11/92

Simple GC Techniques

■ General Concepts■ Indirect Techniques

● Mark-Sweep● Mark-Compact● Copying

■ Direct Techniques● Reference Counting

Page 12: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 12/92

Object

“A container, with a well-defined structure, of one ormore fields, some of which can contain references.”

■ not only full-fledged objects, with encapsulationand inheritance in the context of object-orientedprogramming,

● e.g. instances in the Java language, C++■ but also any kind of structured data records.

● e.g. structs in C

Page 13: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 13/92

Reachability

■ Roots● memory locations that are live by default● e.g. runtime stack locations, static fields● Root Objects

● objects directly reachable from the roots■ Live Objects

● all objects transitively reachable from the roots■ Garbage Objects

● all other objects

Page 14: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 14/92

Reachability Example

StackRuntime Heap

ref

ref

Page 15: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 15/92

GC Phases

■ A GC has two main phases● Identification of garbage objects● Reclamation of garbage objects

■ They are either distinct. . .● Mark-Sweep, Mark-Compact

■ . . . or interleaved● Copying, Reference Counting

Page 16: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 16/92

Fundamental GC Proper ty

“When an object becomes garbage, it stays garbage.”

Page 17: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 17/92

Exact (or Accurate) GC

■ Can tell which memory locations contain obj refs● on stacks, objects, classes (statics)

■ Allows object relocation● will have to update all refs to it

■ Accurate liveness information● exactly all live objs marked

■ Very flexible, but not free!■ Most JVMs have exact GCs (e.g. HotSpot JVM)

Page 18: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 18/92

Conser vative GC

■ Can’t tell which memory locations contain obj refs■ Assume that what looks like an obj ref is an obj ref

● can’t always relocate objects● object liveness information is conservative

■ Easier to implement than exact■ GCs for C/C++■ Some JVMs have semi-conservative GCs

Page 19: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 19/92

Conser vative GC Example

???

StackRuntime Heap

ref

ref

int

Page 20: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 20/92

Mark-Sweep

■ Identify all live objects● marking phase

■ Sweep over heap● de-allocate all garbage objects in-place

■ Allocation● keep track of where free space is

● e.g. free lists, bitmaps● reuse free space to satisfy allocation requests

Page 21: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 21/92

Mark-Sweep Example

Root

1. End Of Marking

Root

0. Mark−Sweep Start

2. End Of Sweeping

Root

Page 22: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 22/92

Mark-Compact

■ Identify all live objects● same as Mark-Sweep

■ Sweep over heap● slide all live objects towards start of the heap● create single free chunk at the end of the heap● need to patch references as objects move

■ Allocation● fast “bump a pointer and check”

Page 23: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 23/92

Mark-Compact Example

1. End Of Marking

0. Mark−Compact Start

2. End Of Compaction

Root

Root

Root

Page 24: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 24/92

Mark-Sweep vs. Mark-Compact

■ Performance● compaction adds 2–2.5 overhead

■ Complexity● Mark-Sweep: well-tuned free lists● Mark-Compact: compaction

■ Fragmentation● Mark-Sweep suffers from it● Mark-Compact eliminates it

Page 25: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 25/92

Copying GC

■ Heap split into two equal-sized areas● from-space and to-space

■ Mutator allocates/modifies objects in from-space■ GC visits transitive closure of live objects. . .

● . . . and copies them to to-space● objects contiguously allocated in to-space● identification / copying interleaved

■ Spaces swap rôles after GC

Page 26: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 26/92

Copying GC Example0. Copying Start

Unused

Unused

Root

Root

1. End Of Copying

Fro

mT

oT

oF

rom

Page 27: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 27/92

Copying GC Performance

✔ Copying GC is very fast. . .● . . . provided the percentage of live objects is low● it only visits live objects

✔ Compaction● no fragmentation and fast allocation

✘ Doubles space requirements though● only from-space used to store live objects● impractical for very large heaps

Page 28: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 28/92

Reference Counting

■ Keep a reference count field per object● increase ref count

● when reference to that object created● decrease ref count

● when reference to that object dropped■ De-allocate objects with zero ref count

● also scan them and decrease ref counts■ Need to track all reference updates

Page 29: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 29/92

Reference Counting Example

2

1

1 1

1

0 1

StackRuntime Heap

ref

ref

!

!

Page 30: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 30/92

Reference Counting Performance

✘ Incomplete● garbage cycles

✘ Incremental, but not always!● last reference to the root of large data structure

✘ Multi-threaded issues● safe ref count maintenance

✘ Extra space requirements

✔ Does not need to visit all objects!

Page 31: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 31/92

Advanced Reference Counting

■ Background cyclic GC■ 2-bit ref counts

● if ref count is 3, assume object live● don’t decrease it after that

● when object garbage, cyclic GC will find it■ Per-thread ref count update buffers

● process them when convenient● schedule de-allocations when convenient

Page 32: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 32/92

Overview

■ Introduction / GC Benefits■ Simple GC Techniques

➜ Incremental GC Techniques■ Generational GC Techniques■ GC in the Java HotSpot Virtual Machine■ GC Issues in the Real World■ Conclusions

Page 33: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 33/92

Incremental GC Techniques

■ Tricolor Marking■ Boehm’s Mostly-Concurrent GC■ Baker’s Copying GC

Page 34: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 34/92

Stop-The-W orld GC

■ Mutator (application) threads stopped during GC● object graph frozen● consistent liveness information

■ Heap inconsistent during object moves● move objects safely when mutator stopped

Page 35: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 35/92

Serial App / Serial GC

Application GC Pause

Time

Page 36: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 36/92

Tricolor Marking

■ Invariant during liveness identification■ An object can have one of three colors

● White: not marked● Gray: marked, its children not yet marked● Black: marked, all its children marked

Page 37: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 37/92

Tricolor Marking

■ Start with all objects white■ Mark roots grey■ While there are gray objects

● pick a gray object● mark its children gray, then mark it black

■ When done, all white objects are garbage

Page 38: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 38/92

Tricolor Marking Example

StackRuntime Heap

ref

ref

Page 39: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 38/92

Tricolor Marking Example

StackRuntime Heap

ref

ref

Page 40: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 38/92

Tricolor Marking Example

StackRuntime Heap

ref

ref

Page 41: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 38/92

Tricolor Marking Example

StackRuntime Heap

ref

ref

Page 42: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 39/92

Incremental GC

■ Perform GC while the mutator is running■ Liveness identification is more challenging

● mutator is changing the object graph● GC might not visit some live objects● need to synchronise GC with mutator

■ The scenario we need to avoid● a black object pointing to a white object

Page 43: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 40/92

Tricolor Invariant Violation

!

Page 44: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 41/92

Mutator/GC Sync hronisation

■ To prevent the violation we can either:■ Track writes to black objects

● with a write barrier● executed upon all reference field updates

● mark them gray■ Track reads from white objects

● with a read barrier● executed upon all reference field reads

● mark them gray

Page 45: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 42/92

Write Barrier

WRITE

Page 46: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 43/92

Read Barrier

READ

Page 47: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 44/92

Boehm’ s Mostl y-Concurrent GC

■ Single-space Incremental Mark-Sweep GC■ GC operation

● first stop-the-world and “checkpoint” roots● incrementally, mark live objects from roots

● keep track of modified reference fields● write barrier (modified black objects gray)

● stop-the-world again and remark● mark from modified reference fields

● incrementally, de-allocate garbage objects

Page 48: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 45/92

Mostl y-Concurrent GC Example

Root

0. Start of GC Cycle

Page 49: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 45/92

Mostl y-Concurrent GC Example

Root

1. Stop−The−World Root Checkpointing

Page 50: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 45/92

Mostl y-Concurrent GC Example

Root

2. Start of Incremental Marking Phase

Page 51: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 45/92

Mostl y-Concurrent GC Example

WRITE

Root

3. Mutator Writes

Page 52: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 45/92

Mostl y-Concurrent GC Example

Root

4. End of Incremental Marking Phase

Page 53: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 45/92

Mostl y-Concurrent GC Example

Root

5. Stop−The−World Remark

Page 54: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 45/92

Mostl y-Concurrent GC Example

Root

6. Incremental Sweeping Phase

Page 55: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 46/92

Baker’ s Incremental Copying GC

■ Two-space Incremental Copying GC■ Mutator only accesses objects in to-space■ GC operation

● background GC copies objects to to-space● when mutator is about to access an object in

from-space● the object is first copied to to-space● read barrier (read white objects gray)

● when all objects copied, swap spaces

Page 56: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 47/92

Incremental Copying GC Example

UnusedTo

Fro

m

Root

0. Start of GC Cycle

Page 57: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 47/92

Incremental Copying GC Example

Root

GC

To

Fro

m

1. Background GC

Page 58: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 47/92

Incremental Copying GC Example

Root

READ

To

Fro

m

2. Mutator Reads

Page 59: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 47/92

Incremental Copying GC Example

Root

WRITE

To

Fro

m

3. Mutator Writes

Page 60: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 47/92

Incremental Copying GC Example

Root

To

Fro

m

Unused

4. End of GC Cycle − Swap Spaces

Page 61: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 48/92

Incremental GC Summar y

■ Pause Times● shorter than stop-the-world, usually!

■ Memory Requirements● greater● floating garbage

■ Total GC Time● 2 or more

■ Complex, hard to debug

Page 62: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 49/92

Overview

■ Introduction / GC Benefits■ Simple GC Techniques■ Incremental GC Techniques

➜ Generational GC Techniques■ GC in the Java HotSpot Virtual Machine■ GC Issues in the Real World■ Conclusions

Page 63: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 50/92

Two Interesting Obser vations

■ Weak Generational Hypothesis● “Most objects will die young.”● “Few refs in old objects point to young objects.”● e.g. ML, SmallTalk, mostly true for Java

programs■ Can take advantage of this

● the GC mostly concentrates on young objects● get more bang for your buck

Page 64: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 51/92

Generational GC

■ Heap split into separate physical areas● generations● objects grouped according to age● N youngest generations GCed independently

■ Need to track references● from older to younger generations● these are assumed to be infrequent!

Page 65: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 52/92

Two-Generation GC

■ Typically● two generations● young generation smaller than old generation

■ Minor Collection● young generation collection, fast, frequent

■ Major Collection● old generation collection, slow, infrequent

Page 66: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 53/92

Generational GC — Promotion

Allocation

Old Generation

Young Generation

Promotion

Page 67: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 54/92

Reference Tracking

■ Write Barrier● filtering / non-filtering

● keeps track of all updated fields, or● only the ones that have old-to-young refs

■ Most widely-used data structures● Card Table● Remembered Set

Page 68: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 55/92

Card Table

■ Heap split into small regions (cards)● an array (card table) has one word per card● cards: 0.5K–2K, clean / dirty● upon a reference field update

● write barrier sets card to dirty● write barrier: 2–3 native instructions

■ Need to scan all fields in all dirty cards■ Works well in multi-threaded environments

Page 69: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 56/92

Card Table Example

Young Generation

Old Generation

Card Table

Page 70: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 57/92

Remembered Set

■ Maintain a list of updated locations● upon a reference field update

● write barrier adds field location to a buffer● can filter unwanted entries, if needed

■ More accurate then card table● heavier-weight write barrier● multi-threaded issues

● per-thread buffers

Page 71: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 58/92

Remembered Set Example

Young Generation

Old Generation

Remembered Set

Page 72: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 59/92

Generational GC Summar y

■ Can combine different GC techniques■ Typical configuration

● Copying GC in young generation● Mark-Compact GC in old generation, or● an Incremental GC in old generation● best of both worlds!

■ More code / memory and write barrier impact . . .■ . . . but collection more effective, in most cases!

Page 73: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 60/92

Overview

■ Introduction / GC Benefits■ Simple GC Techniques■ Incremental GC Techniques■ Generational GC Techniques

➜ GC in the Java HotSpot Virtual Machine■ GC Issues in the Real World■ Conclusions

Page 74: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 61/92

GC in the HotSpot JVM

■ Exact■ Generational with Card Table■ Very, very, very fast allocation■ Stop-The-World and Mostly-Concurrent GCs■ Serial and Parallel GCs

Page 75: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 62/92

Generations in the HotSpot JVM

Old Generation

Young Generation

Unused

Survivor Spaces

Eden

ToFrom

Page 76: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 63/92

Before Minor GC

Old Generation

Young Generation

Unused

Eden

Survivor SpacesFrom To

Page 77: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 64/92

After Minor GC

Old Generation

Young Generation

Unused

Empty

FromTo

Eden

Survivor Spaces

Page 78: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 65/92

Fast Allocation

■ Eden always empty after minor GC● can allocate very fast into it (bump-a-pointer)● allocation code inlined by the JIT● new Object() is about 10 native instructions

■ Multi-threaded allocation● thread-local allocation buffers in eden● no locking for most allocations!

■ Fast allocation is enabled by GC!

Page 79: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 66/92

DEMO

Default GC

Page 80: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 67/92

Java Applications

Server

Workstation

Page 81: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 68/92

Parallel App / Serial GC

Application

Idle!Idle!Idle!Idle!Idle!

GC Pause

Time

Page 82: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 69/92

Parallel GC

■ Parallel GC for young generation● take advantage of multiple CPUs● improves throughput and pause times● load balancing● allows for a larger young generation

■ Old generation still done serially■ Customer quote

“The best JVM enhancement I’ve seen in years!”

Page 83: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 70/92

Parallel App / Parallel GC

Application

Pause Pause

GC

Time

Page 84: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 71/92

DEMO

Parallel GC

Page 85: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 72/92

Pause-Time Issues

✔ Most GC pauses are short● minor collections● parallel young generation

✘ Few GC pauses can be long● major collections● serial● mainly depend on heap size

✘ Some applications cannot tolerate long pauses

Page 86: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 73/92

Mostl y-Concurrent GC

■ Mostly-Concurrent GC for old generation● aka Concurrent Mark-Sweep or CMS● bulk of GC work concurrent (short pauses)● no compaction

● in-place de-allocation● slower promotion, fragmentation

■ Parallel young GC by default, if CPUs available

Page 87: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 74/92

Parallel App / CMS

ApplicationGC

Initial RemarkConc.Mark Conc.Sweep

Time

Page 88: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 75/92

DEMO

Mostl y-Concurrent GC

Page 89: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 76/92

CMS Achievements

■ CMS achieves● low GC pause times

● 200ms–250ms possible● on several GB heaps● in combination with Parallel Young GC

■ It has been successfully deployed● server-style telecommunications applications

■ Works best for 1 CPU, large heaps

Page 90: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 77/92

Overview

■ Introduction / GC Benefits■ Simple GC Techniques■ Incremental GC Techniques■ Generational GC Techniques■ GC in the Java HotSpot Virtual Machine

➜ GC Issues in the Real World■ Conclusions

Page 91: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 78/92

Finalization

■ Cleanup hook for external resources● file descriptors● native GUI state

■ Usage:● override protected void finalize()● at some unspecified time after object has

become unreachable● finalize() might be invoked

Page 92: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 79/92

How Does It Work?

1. An instance is registered when allocated,

2. is enqueued when it becomes unreachable,

3. has its finalize() method invoked,

4. becomes unreachable again,

5. then, finally, has its storage reclaimed.

GC GCMutator finalizer

Page 93: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 80/92

Finalization Impact

✘ Execution speed● slower allocation● finalizer thread affects scheduling

✘ Heap size● memory retained longer

✘ Collection pauses● longer● discovery and queuing

Page 94: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 81/92

Finalization Suggestions

■ Use for cleanup of external resources■ Limit the number of finalizable objects■ Reorganise classes

● finalizable object holds no extra data■ Beware when extending finalizable classes

● in standard libraries (e.g. GUI elements)■ Use one of the java.lang.ref reference

objects instead

Page 95: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 82/92

Object Pools

■ Manual memory management● allocation serialised● current JVMs support fast, parallel allocation

■ Data is kept artificially alive● adds pressure on garbage collector

■ Breaks down abstract data types● who is responsible for the instances?

■ Use if object initialization is really expensive

Page 96: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 83/92

Object Pool Exampleclass Node {

private static Node head = null; private Node next;public static sync hroniz ed Node allocate() {

if (head == null) return new Node();Node result = head; head = head. next; return result;

}public static sync hroniz ed void free(Node n) {

n. next = head; head = n;}. . .

}

Page 97: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 84/92

Real Customer Problem

■ Object pools never truncated● peak live data 300MB● average live data 100MB

■ Problem● other garbage generated from libraries● GCs less frequent, but dealt with 300MB

■ Solution● removed object pools; application ran faster!

Page 98: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 85/92

Avoid Frequent Bad Habits

■ Size heap appropriately● maximum should be larger than working set● leave room for the system to adapt

■ Avoid java.lang.System.gc()● especially when using CMS!

■ Consider setting references to null early● large objects in particular

Page 99: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 86/92

GCs Have Bad Habits Too. . .

✔ The great thing about using a GC is that

It does everything automatically, behind your back!

✘ The bad thing about using a GC is that

It does everything automatically, behind your back!

Page 100: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 86/92

GCs Have Bad Habits Too. . .

✔ The great thing about using a GC is that

It does everything automatically, behind your back!

✘ The bad thing about using a GC is that

It does everything automatically, behind your back!

Page 101: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 87/92

Problem Hunting

■ Most of the time● GC helps the programmer avoid problems

■ When things do go wrong though● problems very hard to track down● lack of feedback from the GC

● everything is automatic, remember?● e.g. the return of the memory leaks

Page 102: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 88/92

Tools

■ Needed● due to the implicit nature of GC

■ Current ones too expensive to use in deploymentenvironments

■ JVMTI● Java Virtual Machine Tool Interface● for development and monitoring tools● JSR 169

Page 103: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 89/92

Overview

■ Introduction / GC Benefits■ Simple GC Techniques■ Incremental GC Techniques■ Generational GC Techniques■ GC in the Java HotSpot Virtual Machine■ GC Issues in the Real World

➜ Conclusions

Page 104: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 90/92

Conc lusions

■ Several types of GC● serial, parallel, concurrent, . . .● each suited to a subset of applications● the HotSpot JVM provides choices● choose the one appropriate for you

■ GC simplifies Java programs● but developers should learn how to use it!

Page 105: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 91/92

Bib liograph y

R. E. Jones. Garbage Collection: Algorithms for AutomaticDynamic Memory Management. John Wiley & Sons, Ltd, 1996.With a chapter on Distributed Garbage Collection by R. Lins.

P. R. Wilson. Uniprocessor Garbage Collection Techniques. InProceedings of the First International Workshop on MemoryManagement, number 637 in Lecture Notes in ComputerScience, pages 1–42, St Malo, France, September 1992.Springer-Verlag.

Page 106: Garbage Collection: Overview, Techniques, Successes · GC in the Java HotSpot Virtual Machine ... really hate garbage collection. GC, University of Tokyo, Sep 22, 2003 9/92 Memory

GC, University of Tokyo, Sep 22, 2003 92/92

QUESTIONS?ton [email protected]