Document

Report
Garbage collector
… an introduction
Peter Varsanyi
Confidential
Agenda
Basics
Memory management
Garbage collector
Garbage collector types
Java garbage collectors
Confidential
2
Garbage Collector
BASICS
Confidential
3
GARBAGE COLLECTOR
Basics
What is it?
• Find data objects in the program, that
cannot be accessed in the future
• Reclaim resources used by these objects
Confidential
4
Basics
Advantages?
• Dangling pointer bugs
• Double free bugs
• Certain type of memory leaks
Confidential
5
Basics
Disadvantages
• Extra computing resources
• Unpredictable collection time
• More memory-related work than useful
work
Confidential
6
Brief history
Automatic garbage collector
• First automatic garbage collection:
LISP(1958)
• Several other languages implemented it:
– BASIC(1964)
– Logo(1967)
– Java 1.0(1996)
Confidential
7
Garbage collector
MEMORY MANAGEMENT
Confidential
8
Memory management
OS level process
• 32 bit: 4GB address space
• 64 bit: 16EB address space
• Kernel space
• User space
0 GB
0x0
2 GB
0x40000000
0x80000000
4 GB
0xC0000000
Confidential
0xFFFFFFFF
9
Memory management
OS level process
• OS + C runtime
• JVM/Native heap
• Java Heap
(-Xmx2GB)
0 GB
2 GB
OS + C runtime JVM NativeHeap
0x0
0x40000000
0x80000000
4 GB
Java heap
0xC0000000
Confidential
0xFFFFFFFF
10
Memory management
• 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
Confidential
11
Memory management
Reference types
• Strong
• Soft
• Weak
• Phantom
Confidential
12
Memory management
Confidential
13
Memory management
Object Lifecycle
Confidential
14
Garbage collector
TYPES
Confidential
15
Garbage collector
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
Confidential
16
Garbage collector types
Tracing collector
• Trace out the heap starting from roots
• Two phases:
– Mark live objects
– Clean dead objects
Confidential
17
Garbage collection
Mark
• Find objects in the heap
• “Paint” them
• Non-marked objects are dead
• Work is linear to “live set”
Confidential
18
Garbage collection
Sweep
• Scans heap for dead objects
• Work is linear to heap size
Confidential
19
Garbage collection
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”
Confidential
20
Garbage collection
Copy
• Moves all objects
• Single pass operation
• Split memory into 2 region
• Work is linear to “live set”
Confidential
21
Java Garbage collectors
Mark/Sweep (1.0)
• Easy to implement
• Reclaim cyclic structures
Confidential
22
Java Garbage collectors
• Generational collection (1.2)
• Young/Eden generation
• Tenured generation
• Perm generation
Confidential
23
Java Garbage collectors
• Generational collection
Confidential
24
Java Garbage collectors
Separate collectors
• Minor GC
– When eden is full
– Sweeps eden + current survivor
• Major GC
– When old is full
– Perm + tenured collected
Confidential
25
Java garbage collectors
Tenured
S1
S2
Eden
Confidential
26
Java Garbage collectors
Confidential
27
Java Garbage collectors
Remembered set
• Track references into young gen
• Card marking
Confidential
28
Java Garbage collectors
Mark/Compact(1.3)
• Combines copy & mark/sweep
• Mark live objects, move these
Confidential
29
Java Garbage collectors
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
Confidential
30
Java Garbage collectors
Serial GC
• Default with 1 CPU
• Young collector: Copy
• Old collector: Mark/Sweep/Compact
• Faster allocation
Confidential
31
Java Garbage collectors
Parallel GC
• Multiple threads to collect young
• More than 2 CPU pause time will be
reduced
• Old gen collected with parallel mark/sweep
Confidential
32
Java Garbage collectors
CMS(Concurrent Mark Sweep)
• Multiple threads to collect young
• Tenured generation: mark/sweep
• Does not compact or copy
• Low pause time
• Occupancy fraction
Confidential
33
Java garbage collectors
CMS phases
1. Initial mark(STW)
2. Concurrent mark
3. Concurrent preclean
4. Remark(STW)
5. Concurrent sweep
6. Concurrent reset
Confidential
34
Java garbage collectors
CMS advantages/disadvantages
• Advantages:
– Low latency
• Disadvantages
– Cannot work when old is full
– No compaction
Confidential
35
Java Garbage collectors
G1 to the rescue
• Most empty region first
• Estimate region clean time
• User defined target time
Confidential
36
Java Garbage collectors
Confidential
37
Java Garbage collectors
G1 to the rescue
• Heap partitioned into equal size of chunks
• More predictable GC pauses
• High throughput
• Compacting collector
Confidential
38
Java Garbage collectors
G1 phases
1. Initial mark(STW)
2. Root region scanning
3. Concurrent marking
4. Remark(STW)(SATB)
5. Clean up
6. Copy(STW)
Confidential
39
Myths
• 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
Confidential
40
Confidential
41
Tools
• Jstat(Part of JDK)
• Java VisualVM(Oracle JDK)
– Visual GC plugin
• -XX:+PrintGC
• -XX:+PrintGCDetails
Confidential
42
Useful links
• http://cscircles.cemc.uwaterloo.ca/java_visu
alize/
• http://www.oracle.com/webfolder/technetwo
rk/tutorials/obe/java/gc01/index.html
• http://www.cubrid.org/blog/devplatform/understanding-java-garbagecollection/
• http://www.slideshare.net/cnbailey/memoryefficient-java
Confidential
43
Confidential
44
Q/A
• Questions?
• No I will not clean your room.
Confidential
45

похожие документы