… an introduction
Peter Varsanyi
Garbage collector
Confidential
Confidential 2
Agenda
Basics
Memory management
Garbage collector
Garbage collector types
Java garbage collectors
Confidential 3
BASICSGarbage 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
Confidential 5
Advantages?
• Dangling pointer bugs• Double free bugs• Certain type of memory leaks
Basics
Confidential 6
Disadvantages
• Extra computing resources• Unpredictable collection time• More memory-related work than useful
work
Basics
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
Confidential 8
MEMORY MANAGEMENT
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
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)
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
Confidential 12
Reference types
• Strong • Soft• Weak• Phantom
Memory management
Confidential 13
Memory management
Confidential 14
Object Lifecycle
Memory management
Confidential 15
TYPESGarbage 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
Confidential 17
Tracing collector
• Trace out the heap starting from roots• Two phases:
– Mark live objects– Clean dead objects
Garbage collector types
Confidential 18
Mark
• Find objects in the heap• “Paint” them• Non-marked objects are dead• Work is linear to “live set”
Garbage collection
Confidential 19
Sweep
• Scans heap for dead objects• Work is linear to heap size
Garbage collection
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
Confidential 21
Copy
• Moves all objects• Single pass operation• Split memory into 2 region• Work is linear to “live set”
Garbage collection
Confidential 22
Mark/Sweep (1.0)
• Easy to implement• Reclaim cyclic structures
Java Garbage collectors
Confidential 23
• Generational collection (1.2)
• Young/Eden generation• Tenured generation• Perm generation
Java Garbage collectors
Confidential 24
• Generational collection
Java Garbage collectors
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
Confidential 26
Java garbage collectors
Tenured S1 S2 Eden
Confidential 27
Java Garbage collectors
Confidential 28
Remembered set
• Track references into young gen• Card marking
Java Garbage collectors
Confidential 29
Mark/Compact(1.3)
• Combines copy & mark/sweep• Mark live objects, move these
Java Garbage collectors
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
Confidential 31
Serial GC
• Default with 1 CPU• Young collector: Copy• Old collector: Mark/Sweep/Compact• Faster allocation
Java Garbage collectors
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
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
Confidential 34
CMS phases1. Initial mark(STW)2. Concurrent mark3. Concurrent preclean4. Remark(STW)5. Concurrent sweep6. Concurrent reset
Java garbage collectors
Confidential 35
CMS advantages/disadvantages
• Advantages:– Low latency
• Disadvantages– Cannot work when old is full– No compaction
Java garbage collectors
Confidential 36
G1 to the rescue
• Most empty region first• Estimate region clean time• User defined target time
Java Garbage collectors
Confidential 37
Java Garbage collectors
Confidential 38
G1 to the rescue
• Heap partitioned into equal size of chunks• More predictable GC pauses • High throughput• Compacting collector
Java Garbage collectors
Confidential 39
G1 phases1. Initial mark(STW)2. Root region scanning3. Concurrent marking4. Remark(STW)(SATB)5. Clean up6. Copy(STW)
Java Garbage collectors
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
Confidential 41
Confidential 42
Tools
• Jstat(Part of JDK)• Java VisualVM(Oracle JDK)
– Visual GC plugin
• -XX:+PrintGC• -XX:+PrintGCDetails
Confidential 43
• http://cscircles.cemc.uwaterloo.ca/java_visualize/
• http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
• http://www.cubrid.org/blog/dev-platform/understanding-java-garbage-collection/
• http://www.slideshare.net/cnbailey/memory-efficient-java
Useful links
Confidential 44
Confidential 45
• Questions?• No I will not clean your room.
Q/A