Download presentation
Presentation is loading. Please wait.
Published byBeatriz Zimmerman Modified over 10 years ago
1
… an introduction Peter Varsanyi Garbage collector Confidential
2
Agenda Confidential 2 Basics Memory management Garbage collector Garbage collector types Java garbage collectors
3
BASICS Garbage Collector Confidential 3
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 4
5
Advantages? Dangling pointer bugs Double free bugs Certain type of memory leaks Basics Confidential 5
6
Disadvantages Extra computing resources Unpredictable collection time More memory-related work than useful work Basics Confidential 6
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 7
8
MEMORY MANAGEMENT Garbage collector Confidential 8
9
OS level process 32 bit: 4GB address space 64 bit: 16EB address space Kernel space User space Memory management Confidential 9 2 GB 0x00x40000000 0x80000000 0xC00000000xFFFFFFFF 4 GB 0 GB
10
OS level process OS + C runtime JVM/Native heap Java Heap Memory management Confidential 10 2 GB 0x00x40000000 0x80000000 0xC00000000xFFFFFFFF 4 GB 0 GB JVM NativeHeap OS + C runtime Java heap (-Xmx2GB)
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 11
12
Reference types Strong Soft Weak Phantom Memory management Confidential 12
13
Memory management Confidential 13
14
Object Lifecycle Memory management Confidential 14
15
TYPES Garbage collector Confidential 15
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 16
17
Tracing collector Trace out the heap starting from roots Two phases: –Mark live objects –Clean dead objects Garbage collector types Confidential 17
18
Mark Find objects in the heap “Paint” them Non-marked objects are dead Work is linear to “live set” Garbage collection Confidential 18
19
Sweep Scans heap for dead objects Work is linear to heap size Garbage collection Confidential 19
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 20
21
Copy Moves all objects Single pass operation Split memory into 2 region Work is linear to “live set” Garbage collection Confidential 21
22
Mark/Sweep (1.0) Easy to implement Reclaim cyclic structures Java Garbage collectors Confidential 22
23
Generational collection (1.2) Young/Eden generation Tenured generation Perm generation Java Garbage collectors Confidential 23
24
Generational collection Java Garbage collectors Confidential 24
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 25
26
Java garbage collectors Confidential 26 Tenured S1S2Eden
27
Java Garbage collectors Confidential 27
28
Remembered set Track references into young gen Card marking Java Garbage collectors Confidential 28
29
Mark/Compact(1.3) Combines copy & mark/sweep Mark live objects, move these Java Garbage collectors Confidential 29
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 30
31
Serial GC Default with 1 CPU Young collector: Copy Old collector: Mark/Sweep/Compact Faster allocation Java Garbage collectors Confidential 31
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 32
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 33
34
CMS phases 1.Initial mark(STW) 2.Concurrent mark 3.Concurrent preclean 4.Remark(STW) 5.Concurrent sweep 6.Concurrent reset Java garbage collectors Confidential 34
35
CMS advantages/disadvantages Advantages: –Low latency Disadvantages –Cannot work when old is full –No compaction Java garbage collectors Confidential 35
36
G1 to the rescue Most empty region first Estimate region clean time User defined target time Java Garbage collectors Confidential 36
37
Java Garbage collectors Confidential 37
38
G1 to the rescue Heap partitioned into equal size of chunks More predictable GC pauses High throughput Compacting collector Java Garbage collectors Confidential 38
39
G1 phases 1.Initial mark(STW) 2.Root region scanning 3.Concurrent marking 4.Remark(STW)(SATB) 5.Clean up 6.Copy(STW) Java Garbage collectors Confidential 39
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 40
41
Confidential 41
42
Tools Jstat(Part of JDK) Java VisualVM(Oracle JDK) –Visual GC plugin -XX:+PrintGC -XX:+PrintGCDetails Confidential 42
43
http://cscircles.cemc.uwaterloo.ca/java_visu alize/http://cscircles.cemc.uwaterloo.ca/java_visu alize/ http://www.oracle.com/webfolder/technetwo rk/tutorials/obe/java/gc01/index.htmlhttp://www.oracle.com/webfolder/technetwo rk/tutorials/obe/java/gc01/index.html http://www.cubrid.org/blog/dev- platform/understanding-java-garbage- collection/http://www.cubrid.org/blog/dev- platform/understanding-java-garbage- collection/ http://www.slideshare.net/cnbailey/memory- efficient-java Useful links Confidential 43
44
Confidential 44
45
Questions? No I will not clean your room. Q/A Confidential 45
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.