garbage collector

45
… an introduction Peter Varsanyi Garbage collector Confidential

Upload: necia

Post on 24-Feb-2016

74 views

Category:

Documents


1 download

DESCRIPTION

Garbage collector. … an intr o duction Peter Varsanyi. Agenda. Garbage Collector. Basics. Garbage collector. What is it? Find data objects in the program, that cannot be accessed in the future Reclaim resources used by these objects. Basics. Advantages? Dangling pointer bugs - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Garbage collector

… an introduction

Peter Varsanyi

Garbage collector

Confidential

Page 2: Garbage collector

Confidential 2

Agenda

Basics

Memory management

Garbage collector

Garbage collector types

Java garbage collectors

Page 3: Garbage collector

Confidential 3

BASICSGarbage Collector

Page 4: Garbage collector

Confidential 4

GARBAGE COLLECTOR

What is it?

• Find data objects in the program, that cannot be accessed in the future

• Reclaim resources used by these objects

Basics

Page 5: Garbage collector

Confidential 5

Advantages?

• Dangling pointer bugs• Double free bugs• Certain type of memory leaks

Basics

Page 6: Garbage collector

Confidential 6

Disadvantages

• Extra computing resources• Unpredictable collection time• More memory-related work than useful

work

Basics

Page 7: Garbage collector

Confidential 7

Automatic garbage collector

• First automatic garbage collection: LISP(1958)

• Several other languages implemented it:– BASIC(1964)– Logo(1967)– Java 1.0(1996)

Brief history

Page 8: Garbage collector

Confidential 8

MEMORY MANAGEMENT

Garbage collector

Page 9: Garbage collector

Confidential 9

OS level process

• 32 bit: 4GB address space• 64 bit: 16EB address space• Kernel space• User space

Memory management

2 GB

0x0 0x40000000 0x80000000 0xC0000000 0xFFFFFFFF

4 GB0 GB

Page 10: Garbage collector

Confidential 10

OS level process

• OS + C runtime• JVM/Native heap• Java Heap

Memory management

2 GB

0x0 0x40000000 0x80000000 0xC0000000 0xFFFFFFFF

4 GB0 GB JVM NativeHeapOS + C runtime Java heap

(-Xmx2GB)

Page 11: Garbage collector

Confidential 11

• Conservative collector– Any bit pattern can be a pointer– No compiler cooperation required

• Precise collector– Require compiler cooperation– Can move objects

• Commercial JVMs use precise collector

Memory management

Page 12: Garbage collector

Confidential 12

Reference types

• Strong • Soft• Weak• Phantom

Memory management

Page 13: Garbage collector

Confidential 13

Memory management

Page 14: Garbage collector

Confidential 14

Object Lifecycle

Memory management

Page 15: Garbage collector

Confidential 15

TYPESGarbage collector

Page 16: Garbage collector

Confidential 16

Reference counting collector

• An early garbage collection strategy• Reference count is maintained in the object• Overhead to increment/decrement

reference count• Cannot clean cyclic references

Garbage collector

Page 17: Garbage collector

Confidential 17

Tracing collector

• Trace out the heap starting from roots• Two phases:

– Mark live objects– Clean dead objects

Garbage collector types

Page 18: Garbage collector

Confidential 18

Mark

• Find objects in the heap• “Paint” them• Non-marked objects are dead• Work is linear to “live set”

Garbage collection

Page 19: Garbage collector

Confidential 19

Sweep

• Scans heap for dead objects• Work is linear to heap size

Garbage collection

Page 20: Garbage collector

Confidential 20

Compact

• Avoid fragmentation• Relocates objects• Remap: fix all references to live objects• End of heap is a large contagious free area• Work is linear to “live set”

Garbage collection

Page 21: Garbage collector

Confidential 21

Copy

• Moves all objects• Single pass operation• Split memory into 2 region• Work is linear to “live set”

Garbage collection

Page 22: Garbage collector

Confidential 22

Mark/Sweep (1.0)

• Easy to implement• Reclaim cyclic structures

Java Garbage collectors

Page 23: Garbage collector

Confidential 23

• Generational collection (1.2)

• Young/Eden generation• Tenured generation• Perm generation

Java Garbage collectors

Page 24: Garbage collector

Confidential 24

• Generational collection

Java Garbage collectors

Page 25: Garbage collector

Confidential 25

Separate collectors

• Minor GC– When eden is full– Sweeps eden + current survivor

• Major GC– When old is full– Perm + tenured collected

Java Garbage collectors

Page 26: Garbage collector

Confidential 26

Java garbage collectors

Tenured S1 S2 Eden

Page 27: Garbage collector

Confidential 27

Java Garbage collectors

Page 28: Garbage collector

Confidential 28

Remembered set

• Track references into young gen• Card marking

Java Garbage collectors

Page 29: Garbage collector

Confidential 29

Mark/Compact(1.3)

• Combines copy & mark/sweep• Mark live objects, move these

Java Garbage collectors

Page 30: Garbage collector

Confidential 30

Concepts

• Parallel• Uses multiple CPU to perform collection at the same time

• Concurrent• Performs GC concurrently with application’s own execution

• Safe point• Point in thread execution, where collector can identify all

references in thread’s execution stack

Java Garbage collectors

Page 31: Garbage collector

Confidential 31

Serial GC

• Default with 1 CPU• Young collector: Copy• Old collector: Mark/Sweep/Compact• Faster allocation

Java Garbage collectors

Page 32: Garbage collector

Confidential 32

Parallel GC

• Multiple threads to collect young• More than 2 CPU pause time will be

reduced• Old gen collected with parallel mark/sweep

Java Garbage collectors

Page 33: Garbage collector

Confidential 33

CMS(Concurrent Mark Sweep)

• Multiple threads to collect young• Tenured generation: mark/sweep• Does not compact or copy• Low pause time• Occupancy fraction

Java Garbage collectors

Page 34: Garbage collector

Confidential 34

CMS phases1. Initial mark(STW)2. Concurrent mark3. Concurrent preclean4. Remark(STW)5. Concurrent sweep6. Concurrent reset

Java garbage collectors

Page 35: Garbage collector

Confidential 35

CMS advantages/disadvantages

• Advantages:– Low latency

• Disadvantages– Cannot work when old is full– No compaction

Java garbage collectors

Page 36: Garbage collector

Confidential 36

G1 to the rescue

• Most empty region first• Estimate region clean time• User defined target time

Java Garbage collectors

Page 37: Garbage collector

Confidential 37

Java Garbage collectors

Page 38: Garbage collector

Confidential 38

G1 to the rescue

• Heap partitioned into equal size of chunks• More predictable GC pauses • High throughput• Compacting collector

Java Garbage collectors

Page 39: Garbage collector

Confidential 39

G1 phases1. Initial mark(STW)2. Root region scanning3. Concurrent marking4. Remark(STW)(SATB)5. Clean up6. Copy(STW)

Java Garbage collectors

Page 40: Garbage collector

Confidential 40

• Creating object has high cost• Do not create lots of small objects• No more memory leak(MAGIC!)• Increase the heap, what could happen?• Null everything you don’t use• System.gc() will collect everything

immediately

Myths

Page 41: Garbage collector

Confidential 41

Page 42: Garbage collector

Confidential 42

Tools

• Jstat(Part of JDK)• Java VisualVM(Oracle JDK)

– Visual GC plugin

• -XX:+PrintGC• -XX:+PrintGCDetails

Page 44: Garbage collector

Confidential 44

Page 45: Garbage collector

Confidential 45

• Questions?• No I will not clean your room.

Q/A