460 likes | 812 Views
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
E N D
Garbage collector … an introduction Peter Varsanyi Confidential
Agenda Confidential
Garbage Collector Basics Confidential
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
Advantages? Dangling pointer bugs Double free bugs Certain type of memory leaks Basics Confidential
Disadvantages Extra computing resources Unpredictable collection time More memory-related work than useful work Basics Confidential
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
Garbage collector Memory management Confidential
OS level process 32 bit: 4GB address space 64 bit: 16EB address space Kernel space User space Memory management 0 GB 2 GB 4 GB 0x0 0x40000000 0xC0000000 0xFFFFFFFF 0x80000000 Confidential
OS level process OS + C runtime JVM/Native heap Java Heap Memory management (-Xmx2GB) 0 GB 2 GB 4 GB Java heap OS + C runtime JVM NativeHeap 0x0 0x40000000 0xC0000000 0xFFFFFFFF 0x80000000 Confidential
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
Reference types Strong Soft Weak Phantom Memory management Confidential
Memory management Confidential
Object Lifecycle Memory management Confidential
Garbage collector Types Confidential
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
Tracing collector Trace out the heap starting from roots Two phases: • Mark live objects • Clean dead objects Garbage collector types Confidential
Mark Find objects in the heap “Paint” them Non-marked objects are dead Work is linear to “live set” Garbage collection Confidential
Sweep Scans heap for dead objects Work is linear to heap size Garbage collection Confidential
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
Copy Moves all objects Single pass operation Split memory into 2 region Work is linear to “live set” Garbage collection Confidential
Mark/Sweep (1.0) Easy to implement Reclaim cyclic structures Java Garbage collectors Confidential
Generational collection (1.2) Young/Eden generation Tenured generation Perm generation Java Garbage collectors Confidential
Generational collection Java Garbage collectors Confidential
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
Java garbage collectors Eden S1 S2 Tenured Confidential
Java Garbage collectors Confidential
Remembered set Track references into young gen Card marking Java Garbage collectors Confidential
Mark/Compact(1.3) Combines copy & mark/sweep Mark live objects, move these Java Garbage collectors Confidential
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
Serial GC Default with 1 CPU Young collector: Copy Old collector: Mark/Sweep/Compact Faster allocation Java Garbage collectors Confidential
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
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
CMS phases Initial mark(STW) Concurrent mark Concurrent preclean Remark(STW) Concurrent sweep Concurrent reset Java garbage collectors Confidential
CMS advantages/disadvantages Advantages: • Low latency Disadvantages • Cannot work when old is full • No compaction Java garbage collectors Confidential
G1 to the rescue Most empty region first Estimate region clean time User defined target time Java Garbage collectors Confidential
Java Garbage collectors Confidential
G1 to the rescue Heap partitioned into equal size of chunks More predictable GC pauses High throughput Compacting collector Java Garbage collectors Confidential
G1 phases Initial mark(STW) Root region scanning Concurrent marking Remark(STW)(SATB) Clean up Copy(STW) Java Garbage collectors Confidential
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
Tools Jstat(Part of JDK) Java VisualVM(Oracle JDK) • Visual GC plugin -XX:+PrintGC -XX:+PrintGCDetails Confidential
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
Questions? No I will not clean your room. Q/A Confidential