Java Performance Fundamental 세미나 교재입니다. 3장은 Garbage Collection에 대해 설명하고 있습니다. 먼저 JVM에서 사용하는 Garbage Collection 뿐만 아니라 일반적인 Garbage Collection의 Algorithm을 상세히 알아봅니다. 그 후 Hotspot JVM의 Heap구조와 Garbage Collector를 설명하고 Generation별로 어떻게 동작하는 지에 대해 설명합니다. 이어 IBM JVM에 대한 Heap 구조와 Garbage Collector에 대해서도 설명합니다.
* Garbage Collection 이란?
* Garbage Collection 의 대상
* Garbage Collection 의 기본 Algorithm
* Hotspot JVM의 Garbage Collection
o Serial Collector
o Incremental Collector
o Parallel Collector
o CMS Collector
o Parallel Compaction Collector
o Garbage First Collector
* IBM JVM의 Garbage Collection
o Optimize for Throughput Collector
o Optimize for Pause Time Collector
o Generational Concurrent Collector
o Subpool Collector
13. Java Performance
7
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection
ROOT SET
14. Java Performance
7
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection
ROOT SET
Local Variable,
Operand Stack
15. Java Performance
7
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection
ROOT SET
Local Variable,
Operand Stack
Class
Constant Pool
Reference
16. Java Performance
7
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection
ROOT SET
Local Variable, Unreleased Native
Operand Stack Method
Object
Reference
Class
Constant Pool
Reference
17. Java Performance
8
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Colletion
• Root Set
18. Java Performance
9
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Colletion
• Memory Leak
class Main{
public static void main (String args[]) {
Leak lk = new Leak();
for(int a=0; a<9000000; a++) {
lk.addList(a);
lk.removeStr(a);
}
}
}
class Leak {
ArrayList lst = new ArrayList();
public void addList(int a) {
lst.add(" "+a);
}
public void removeStr(int i) {
Object obj = lst.get(i);
obj = null;
}
}
19. Java Performance
9
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Colletion
• Memory Leak
class Main{
public static void main (String args[]) {
Leak lk = new Leak();
for(int a=0; a<9000000; a++) {
lk.addList(a);
lk.removeStr(a);
}
} Why leak?
}
class Leak {
ArrayList lst = new ArrayList();
public void addList(int a) {
lst.add(" "+a);
}
public void removeStr(int i) {
Object obj = lst.get(i);
obj = null;
}
}
28. Java Performance
15
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Integer
Object a = new Ingeter(1);
RefCnt 00
Integer
RefCnt 00
Integer
RefCnt 00
29. Java Performance
15
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Integer
Object a = new Ingeter(1); a 1
RefCnt 01
0
Integer
RefCnt 00
Integer
RefCnt 00
30. Java Performance
15
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Integer
Object a = new Ingeter(1); a 1
Object b = a; RefCnt 01
0
Integer
RefCnt 00
Integer
RefCnt 00
31. Java Performance
15
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Integer
Object a = new Ingeter(1); a 1
Object b = a; b RefCnt 01
0
2
Integer
RefCnt 00
Integer
RefCnt 00
32. Java Performance
15
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Integer
Object a = new Ingeter(1); a 1
Object b = a; b RefCnt 01
0
2
Integer
Object a = new Ingeter(1); RefCnt 00
Integer
RefCnt 00
33. Java Performance
15
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Integer
Object a = new Ingeter(1); a 1
Object b = a; b RefCnt 01
0
2
Integer
a 1
Object a = new Ingeter(1); 0
RefCnt 01
Integer
RefCnt 00
34. Java Performance
15
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Integer
Object a = new Ingeter(1); a 1
Object b = a; b RefCnt 01
0
2
Integer
a 1
Object a = new Ingeter(1); 0
RefCnt 01
Object b = new Ingeter(2); Integer
RefCnt 00
35. Java Performance
15
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Integer
Object a = new Ingeter(1); a 1
Object b = a; b RefCnt 01
0
2
Integer
a 1
Object a = new Ingeter(1); 0
RefCnt 01
Object b = new Ingeter(2); Integer
2
b RefCnt 01
0
36. Java Performance
15
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Integer
Object a = new Ingeter(1); a 1
Object b = a; b RefCnt 01
0
2
Integer
a 1
Object a = new Ingeter(1); 0
RefCnt 01
Object b = new Ingeter(2); Integer
a = b; 2
b RefCnt 01
0
37. Java Performance
15
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Integer
Object a = new Ingeter(1); a 1
Object b = a; b RefCnt 01
0
2
Integer
a 1
Object a = new Ingeter(1); 0
RefCnt 01
Object b = new Ingeter(2); Integer
a = b; 2
b RefCnt 01
0
2
46. Java Performance
18
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
a
Linked List
next ref
RefCnt 02
Linked List
next ref
RefCnt 11
Linked List
next ref
RefCnt 01
47. Java Performance
18
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
a
Linked List
next ref
2
RefCnt 01
Linked List
next ref
RefCnt 11
Linked List
next ref
RefCnt 01
48. Java Performance
19
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
• Mark-and-Sweep Algorithm
– Root Set Reference
– Tracing Algorithm
– Mark Sweep
• Mark : Live Object Mark
(Object flag or bitmap table )
• Sweep : Heap Mark
70. Java Performance
26
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
• Copying Algorithm
– Stop-the-Copy Algorithm
– Heap Active Inactive
– Active Object
– Active Live Object Inactive
Copy
71. Java Performance
27
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Active Area
A C
B
R
Inactive Area
72. Java Performance
27
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Active Area
A C
null null
B
null
R
Inactive Area
73. Java Performance
27
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Active Area
A C
null
B
null
R
A’
null
Inactive Area
74. Java Performance
27
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Active Area
A C
null
B
R
A’ B’
null null
Inactive Area
75. Java Performance
27
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Active Area
A C
B
R
A’ B’ C’
null null null
Inactive Area
76. Java Performance
27
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Inactive Area
R
A’ B’ C’
null null null
Active Area
78. Java Performance
29
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
• Generational Algorithm
– Copying Algorithm
• Copy
• Object
•
Object
• Long Lived Object
– Heap age sub heap
Youngest Generation Sub heap GC
– Object Age
79. Java Performance
30
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Youngest Generation Sub Heap
Matured Live Live Dead Dead
Live Matured Dead Live Dead
Dead Live Dead Dead Matured
Tenured Generation Sub Heap
80. Java Performance
30
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Youngest Generation Sub Heap
Live Live Dead Dead
Live Dead Live Dead
Dead Live Dead Dead
Tenured Generation Sub Heap GC
Matured Matured Matured
81. Java Performance
30
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Youngest Generation Sub Heap
Live Live
Live Live
Live
Tenured Generation Sub Heap GC
Matured Matured Matured
82. Java Performance
30
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Youngest Generation Sub Heap
Matured Matured Dead
Live
Matured Dead
Live Matured Live
Tenured Generation Sub Heap
Matured Matured Matured
85. Java Performance
33
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
• Train Algorithm
Car : Memory Block , Fixed Size
RememberSet RememberSet
Car root
A B C A’ B’ C’ set
Train : Car( ) Car
86. Java Performance
34
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Car 1-1 Car 1-2
Train 1 {R1,E}
A B C D E F
R1 Car 2-1
Train 2 {R2,B}
R2
G
87. Java Performance
34
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Free Car Car 1-1 Car 1-2
Train 1 {C}
D E F C
R1 Car 2-1
Train 2 {R2,B}
R2
G
Car 3-1
Train 3 {R1}
A B
88. Java Performance
34
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Garbage Collection Algorithm
Free Car Free Car Free Car
Free Train
R1 Car 2-1
Train 2 {R2,B}
R2
G
Car 3-1
Train 3 {R1}
A B
93. Java Performance
39
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Garbage Collection of Hotspot JVM
– Weak Generational Hypothesis
• High Infant Mortality
• Few References from Older to Younger Objects
Exists
– Young Generation GC Algorithm
• Speed – Fast Allocation & TLAB
– Old Generation GC Algorithm
• – Card Table & Write Barrier
94. Java Performance
40
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Fast Allocation & TLAB
T T T T T Synchronization
1 2 3 4 5 Wait
Allocation
T T T T T
1 2 3 4 5
TLAB Allocation Allocation Allocation Allocation Allocation
96. Java Performance
42
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Card Table Write Barrier
– 1 Byte Card / 512 Byte of Old Generation
– Old to Young Reference
– Write Barrier
• Bytecode Interpreter
• 2 native instructions
Young Generation
Old Generation
Card Table
97. Java Performance
43
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Garbage Collection of Hotspot JVM
– Garbage Collection
• Minor Collection : Young Generation
• Major Collection : Old Generation
– Full Collection
– Method Area GC Major Full
98. Java Performance
44
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Heap of Hotspot JVM
Survivor 1
Survivor 2
Eden Tenured Permanent
Young Generation Old Generation Method Area
GC
109. Java Performance
55
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Garbage Collector
– Garbage First Collector
• Java SE 6 Update 14
• Train Algorithm
• Generation
• Heap Region
Young , Old Area
• Realtime Low Pause
110. Java Performance
56
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Serial Collector
– Young, Old Generation Serial
(Single CPU Use)
– Suspend during Collecting
– Client Class Collector
–
111. Java Performance
57
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Serial Collector
– Young Generation Collection :
Generational
Young Eden
A
Survivor1 - From Survivor2 - To
B C Empty
Old
112. Java Performance
57
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Serial Collector
– Young Generation Collection :
Generational
Minor GC
Young Eden
A
Survivor1 - From Survivor2 - To
B C A’ B’ C’
Old
113. Java Performance
57
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Serial Collector
– Young Generation Collection :
Generational
Young Eden
Empty
Survivor1 - From Survivor2 - To
Empty A’ B’ C’
Old
114. Java Performance
58
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Serial Collector
Young Eden
A
Survivor1 - To Survivor2 - From
Empty B M
Old
115. Java Performance
58
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Serial Collector
Minor GC
Young Eden
Empty
Survivor1 - To Survivor2 - From
A’ B’ Empty
Old
M’
116. Java Performance
59
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Serial Collector
– Old Generation Collection
: Mark-and-Compacting
Old
117. Java Performance
59
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Serial Collector
– Old Generation Collection
: Mark-and-Compacting
Mark
Old
√ √ √
118. Java Performance
59
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Serial Collector
– Old Generation Collection
: Mark-and-Compacting
Sweep
Old
√ √ √
119. Java Performance
59
Java Performance Fundamental | twitter @novathinker
artdb@ex-em.com | performeister.tistory.com
Hotspot JVM
• Serial Collector
– Old Generation Collection
: Mark-and-Compacting
Sliding Compaction
Old
√ √ √
Sliding Compaction : Fragmentation