Garbage Collection for Dummies

58
Copyright © 2012 Akira Koyasu. Some rights reserved. Garbage Collection for Dummies Overview HotSpot GC photo#1

description

Overview HotSpot Garbage Collection. GC fundamentals, the HotSpot garbage collectors and the start point of GC tuning.

Transcript of Garbage Collection for Dummies

Page 1: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Garbage Collection for DummiesOverview HotSpot GC

photo#1

Page 2: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

AgendaJVM & GC heap & GC fundamentals

Collectors in HotSpot JVM HotSpot Collectors explained

Ergonomics & Tuning the start point of GC tuning

2

Page 3: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

JVM & GC

heap in JVM

Garbage Collection

stop the world

Generational GC

3

Page 4: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

heap in JVM

4

public static void main(String[] args) { int i = 1234; long[] longs = new long[16]; String str = "Hello!!"; Simple s = new Simple();}

Which objects are allocated in heap?

Page 5: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.5

javap -c

Page 6: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.5

$ javap -c study.gc.bootstrap.HeapCompiled from "Heap.java"public class study.gc.bootstrap.Heap { ... public static void main(java.lang.String[]); Code: 0: sipush 1234 3: istore_1 4: bipush 16 6: newarray long 8: astore_2 9: ldc #16 // String Hello!! 11: astore_3 12: new #18 // class study/gc/Simple 15: dup 16: invokespecial #20 // Method study/gc/Simple."<init>":()V 19: astore 4 21: return }

javap -c

Page 7: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Garbage Collection

6

“Our resource is limited.So garbages should be

collected and recycled as soon as possible”

Page 8: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Dead or Alive

7

void main() { sub();}void sub() { Greeter g = new Greeter(); Message msg; msg = new Message("Morning!"); g.doGreet(msg); msg = new Message("Hello!"); g.doGreet(msg);}

Greeter

Message

Message

at the end of each method, which objects are dead?

Page 9: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Dead or Alive

8

Unreferenced objects are dead

reassignment

scope out

Page 10: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Fragment, Compaction, Allocation

9

A CB ABB B

Page 11: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Fragment, Compaction, Allocation

9

A CB A

fragment

Page 12: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Fragment, Compaction, Allocation

9

A CB A

Page 13: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Fragment, Compaction, Allocation

9

A CB A

Compaction

Page 14: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Fragment, Compaction, Allocation

9

A CB A

Page 15: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Fragment, Compaction, Allocation

9

A CB A

Allocation

Page 16: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Fragment, Compaction, Allocation

9

A CB A C

Page 17: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Live object identification

10

Reference counting

Traverse marking from root

Postman

Message

Message

Bag

Page 18: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

stop the world

11

Postman

Message

Message

Bag

In traversing, the Postman get a new message!!

Page 19: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

stop the world

11

Postman

Message

Message

Bag

In traversing, the Postman get a new message!!

Page 20: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

stop the world

11

Postman

Message

Message

Bag

In traversing, the Postman get a new message!!

Message

Page 21: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Generational GC

12

the weak generational hypothesis:“Most objects die young”

young -> frequently collected

old -> seldomly collected

Page 22: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Partitioned into generations

13

C

B

AEden

Survivor

Old

Young{

Page 23: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Collectors in HotSpot JVM

throughput & low-latency

5 Collectors

14

Page 24: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

throughput & low-latency

throughput: more tasks in unit time

low-latency: fast response

15

Page 25: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

5 collectors

Serial Collector

Parallel Collector

Parallel Compacting Collector

Concurrent Mark Sweep Collector

Garbage First Collctor (1.7_u4)

16

Page 26: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Serial Collector

1712

Eden

Survivor

Old

From To

for young generationcopying

Page 27: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Serial Collector

1812

for old generation

mark-sweep

Page 28: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Parallel Collector

19

for young generation

use a parallel version of the serial collector algorithm

Serial Collector

Parallel Collector

Page 29: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Parallel Compacting Collector

20

for old generation

R1 R2 R3 R4 R5

Page 30: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Parallel Compacting Collector

20

for old generation

R1 R2 R3 R4 R5

T1 T2

Page 31: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Parallel Compacting Collector

20

for old generation

R1 R2 R3 R4 R5

T1 T2

Page 32: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Parallel Compacting Collector

20

for old generation

R1 R2 R3 R4 R5

T1 T2

Page 33: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Parallel Compacting Collector

20

for old generation

R1 R2 R3 R4 R5

T1 T2

Page 34: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Parallel Compacting Collector

20

for old generation

R1 R2 R3 R4 R5

T1 T2

Page 35: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Concurrent Mark Sweep Collector

21

for old generation

non-compactingPhases

initial mark (STW)concurrent markremark (STW)concurrent sweep

Page 36: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Concurrent Mark Sweep Collector

22

for old generationPostman

Message

Message

Bag

new Message is marked in remark phase

Bag is updated in concurrent marking phase

Page 37: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Concurrent Mark Sweep Collector

22

for old generationPostman

Message

Message

Bag

new Message is marked in remark phase

Bag is updated in concurrent marking phase

Page 38: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Concurrent Mark Sweep Collector

22

for old generationPostman

Message

Message

Bag

Message

new Message is marked in remark phase

Bag is updated in concurrent marking phase

Page 39: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Concurrent Mark Sweep Collector

22

for old generationPostman

Message

Message

Bag

Message

new Message is marked in remark phase

Bag is updated in concurrent marking phase

Page 40: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Garbage First Collector

23

for young & old generation

Y

Y O

Y O

O O

heap is devided into regions

some regions are young, some other regions are old

Page 41: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Garbage First Collector

24

for young & old generation

collection regions chosen based on time prediction

(some garbages remain)

Page 42: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Garbage First Collector

24

for young & old generation

collection regions chosen based on time prediction

(some garbages remain)

Page 43: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Garbage First Collector

24

for young & old generation

collection regions chosen based on time prediction

(some garbages remain)

Page 44: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

select collector

25

collector optionSerial -XX:+UseSerialGC

Parallel -XX:+UseParallelGC

Parallel Compacting -XX:+UseParallelOldGC

CMS -XX:+UseConcMarkSweepGC

G1 -XX:+UseG1GC

Page 45: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Ergonomics & Tuning

Ergonomics

JVM options

command line tools

26

Page 46: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

GC tuning

27

the first step to tune GC is to do nothing

Page 47: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Ergonomics

28

latency: -XX:MaxGCPauseMillis=n

throughput: -XX:GCTimeRatio=99

Behavior-based options

Page 48: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

JVM options

-Xmxn

-Xmsn

-XX:ParallelGCThreads=n

-XX:+PrintFlagsFinal

29

Page 49: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

command line tools

jps

jstat

jmap

jhat

30

Page 50: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

jstat

31

Page 51: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

jstat

31

$ jps -l23437 study.gc.bootstrap.Heap23489 sun.tools.jps.Jps

$ jstat -gcutil -h20 23437 1s S0 S1 E O P YGC YGCT FGC FGCT GCT 50.00 0.00 94.64 0.56 12.63 32 0.054 0 0.000 0.054 0.00 0.00 12.08 0.56 12.63 35 0.054 0 0.000 0.054 50.00 0.00 0.00 0.56 12.63 38 0.090 0 0.000 0.090 0.00 0.00 0.00 0.56 12.63 40 0.092 0 0.000 0.092 0.00 25.00 74.50 0.56 12.63 41 0.092 0 0.000 0.092 0.00 0.00 22.15 0.56 12.63 43 0.093 0 0.000 0.093 0.00 25.00 10.07 0.56 12.63 45 0.093 0 0.000 0.093 0.00 25.00 0.00 0.56 12.63 47 0.094 0 0.000 0.094 0.00 25.00 0.00 0.56 12.63 49 0.096 0 0.000 0.096 0.00 0.00 24.16 0.56 12.63 51 0.098 0 0.000 0.098 0.00 0.00 42.28 0.56 12.63 53 0.099 0 0.000 0.099 0.00 0.00 44.29 0.56 12.63 55 0.100 0 0.000 0.100...

Page 52: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

jmap

32

Page 53: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

jmap

32

$ jmap -histo:live 23437

num #instances #bytes class name---------------------------------------------- 1: 5822 797008 <methodKlass> 2: 5822 699712 <constMethodKlass> 3: 393 456488 <constantPoolKlass> 4: 360 286400 <constantPoolCacheKlass> 5: 393 283512 <instanceKlassKlass> 6: 1094 98656 [C 7: 511 97864 [B 8: 452 55128 java.lang.Class 9: 632 40536 [[I 10: 589 37440 [S...

$ jmap -dump:live,format=b,file=heap.dump 23437Dumping heap to heap.dump ...Heap dump file created

Page 54: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

jhat

33

Page 55: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

jhat

33

$ jhat heap.dump Reading from heap.dump...Dump file created Thu Dec 27 15:44:54Snapshot read, resolving...Resolving 5895 objects...Chasing references, expect 1 dots.Eliminating duplicate references.Snapshot resolved.Started HTTP server on port 7000Server is ready.

Page 56: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

jhat

33

$ jhat heap.dump Reading from heap.dump...Dump file created Thu Dec 27 15:44:54Snapshot read, resolving...Resolving 5895 objects...Chasing references, expect 1 dots.Eliminating duplicate references.Snapshot resolved.Started HTTP server on port 7000Server is ready.

Page 57: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

References

34

Memory Management in the Java HotSpot Virtual Machine (Sun Microsystems, April 2006)

Garbage-First Garbage Collection (Sun Microsystems, October 2004)

A Generational Mostly-concurrent Garbage Collector (Sun Microsystems, June 2000)

Garbage-First Collector http://docs.oracle.com/javase/7/docs/technotes/guides/vm/G1.html

Parallel Compaction http://docs.oracle.com/javase/7/docs/technotes/guides/vm/par-compaction-6.html

Concurrent Mark Sweep Collector Enhancements http://docs.oracle.com/javase/7/docs/technotes/guides/vm/cms-6.html

Storage Management http://openjdk.java.net/groups/hotspot/docs/StorageManagement.html

Page 58: Garbage Collection for Dummies

Copyright © 2012 Akira Koyasu. Some rights reserved.

Notes

35

This work is licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/.

photo#1 http://www.flickr.com/photos/yto/5896836025/