Top Banner
78

The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

May 27, 2018

Download

Documents

vukhuong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK
Page 2: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK
Page 3: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

The G1 GC in JDK 9

Erik DuvebladSenior Member of Technical StafOracle JVM GC TeamOctober, 2017

3

Page 4: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Safe Harbor Statement

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

4

Page 5: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

The G1 Garbage Collector in JDK 9

Quick intro to G1 in 2017

Five major improvements since JDK 8

The future

1

2

3

5

Page 6: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Who am I?

• Erik Duveblad

• Master’s thesis on load balancing during GC

• Have worked with GC at Oracle for ~6 years

• Worked with the G1 GC for past 3 years

• > 150 commits to OpenJDK (Reviewer)

Page 7: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

The G1 Garbage Collector in JDK 9

Quick intro to G1 in 2017

Five major improvements since JDK 8

The future

1

2

3

7

Page 8: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• Garbage collection (GC) is a way to manage memory

• In Java, programmers do not need to explicitly manage memory:

• Compared to C where programmers must manage the memory:

What is garbage collection?

8

// Memory reclaimed by the GCString s = new String(“Hello GC!”);

char* s = malloc(sizeof(char)*10);

// Memory reclaimed by programmerfree(s);

Page 9: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• How does GC work?

What is garbage collection?

9

Heap

Page 10: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• How does GC work?

What is garbage collection?

10

Heap

Objects

Page 11: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• How does GC work?

What is garbage collection?

11

Heap

Objects

Fields

...

Page 12: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• How does GC work?

What is garbage collection?

12

Heap

Objects

Fields

References

Page 13: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• How does GC work?

What is garbage collection?

13

Heap

Objects

Fields

References

Page 14: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• How does GC work?

What is garbage collection?

14

Heap

Objects

Fields

References

Roots

Page 15: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• How does GC work?

What is garbage collection?

15

Heap

Objects

Fields

References

Roots

Page 16: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• How does GC work?

What is garbage collection?

16

Heap

Objects

Fields

References

Roots

Page 17: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• How does GC work?

What is garbage collection?

17

Heap

Objects

Fields

References

Roots

Page 18: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• How does GC work?

What is garbage collection?

18

Heap

Compaction

Page 19: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017What is G1?

19

Page 20: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

Be wary when reading old blog posts, old documentation, etc.

What is G1?

20

2004

Paper published

2009

Experimental support(JDK 6u14)

2012

Official support(JDK 7u4)

Default(JDK 9)

2017

Page 21: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• The goal: throughput and low latency– Throughput – number of transactions per second

– Latency – maximum time of a transaction

• The default pause goal is 200 milliseconds

– Higher pause goal → more throughput, higher latency

– Lower pause goal → less throughput, lower latency

What is G1?

21

Page 22: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• The heap is split into multiple regions

• Region size depends on heap size, e.g. 2 MB for 4 GB heap

Generational region-based memory management

22

Heap

Page 23: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• New objects are allocated into eden (E) regions

Generational region-based memory management

23

E

E

E

Heap

Page 24: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• A young collection happens after a number of eden regions have been allocated

Generational region-based memory management

24

E

E

E

Heap

Page 25: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• Young collections compactly copy live objects in eden regions tosurvivor regions (S)

Generational region-based memory management

25

E S

E

E

Heap

Page 26: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• Objects will then continue to be allocated in eden regions

Generational region-based memory management

26

E S

E

E

Heap

Page 27: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• If objects survive multiple young collections, then they are compactly copied into an old region (O)

Generational region-based memory management

27

E S

S E

E

O

Heap

Page 28: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• After a while the heap fills up with eden, survivor and old regions

Generational region-based memory management

28

E O

O

O S O

S

O

E

E

EO

O

O

Heap

Page 29: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• All live objects in old regions are then marked concurrently

• The Java application is not stopped

Generational region-based memory management

29

E O

O

O S O

S

O

E

E

EO

O

O

Heap

Page 30: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• Eden, survivor and old regions are then collected in mixed collections.

• Live objects are compactly copied into survivor and old regions.

Generational region-based memory management

30

E O

O

O S O

S

O

E

E

EO

O

O

Heap

Page 31: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• Eden, survivor and old regions are then collected in mixed collections.

• Live objects are compactly copied into survivor and old regions.

Generational region-based memory management

31

E O

O O

S

O S O

S

O

E

E

EO

O

O

Heap

Page 32: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• Eden, survivor and old regions are then collected in mixed collections.

• Live objects are compactly copied into survivor and old regions.

Generational region-based memory management

32

O

S

O O

OO

O

O

Heap

Page 33: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• Eden, survivor and old regions are then collected in mixed collections.

• Live objects are compactly copied into survivor and old regions.

Generational region-based memory management

33

O

S O

O

O

O

Heap

Page 34: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

• When no more old regions are suitable for collection,then G1 will resume doing young collections

Generational region-based memory management

34

O

O

S

O

Heap

Page 35: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Quick intro to G1 in 2017

● G1 thus transitions between the following states

Generational region-based memory management

35

YC + CMYC

MC

Young collections

Young collections +concurrent mark

Mixed collections

Page 36: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

The G1 Garbage Collector in JDK 9

Quick intro to G1 in 2017

Five major improvements since JDK 8

The future

1

2

3

36

Page 37: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Five major improvements to G1 since JDK 8

1) String deduplication (JDK 8u20)

2) Class unloading with concurrent mark (JDK 8u40)

3) Eagerly reclaim humongous regions (JDK 8u60)

4) Adaptive start of concurrent mark (JDK 9)

5) More efficient collections (JDK 9)

37

Page 38: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Five major improvements to G1 since JDK 8

1) String deduplication (JDK 8u20)

2) Class unloading with concurrent mark (JDK 8u40)

3) Eagerly reclaim humongous regions (JDK 8u40)

4) Adaptive start of concurrent mark (JDK 9)

5) More efficient collections (JDK 9)

38

Page 39: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

String deduplication (JDK 8u20)

39

char[] value

int hash

j.l.String

‘J’ ‘a’ ‘v’ ‘a’ ‘O’ ‘n’ ‘e’

char[]

length: 7

char[] value

int hash

j.l.String

‘J’ ‘a’ ‘v’ ‘a’ ‘O’ ‘n’ ‘e’

char[]

length: 7

String conf = new String(“JavaOne”);

String j1 = new String(“JavaOne”);

String conf = new String(“JavaOne”);

String j1 = new String(“JavaOne”);

confconf

j1j1

Page 40: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

String deduplication (JDK 8u20)

40

char[] value

int hash

j.l.String

‘J’ ‘a’ ‘v’ ‘a’ ‘O’ ‘n’ ‘e’

char[]

length: 7

char[] value

int hash

j.l.String

‘J’ ‘a’ ‘v’ ‘a’ ‘O’ ‘n’ ‘e’

char[]

length: 7

String conf = new String(“JavaOne”);

String j1 = new String(“JavaOne”);

String conf = new String(“JavaOne”);

String j1 = new String(“JavaOne”);

confconf

j1j1

??

Page 41: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

String deduplication (JDK 8u20)

41

char[] value

int hash

j.l.String

‘J’ ‘a’ ‘v’ ‘a’ ‘O’ ‘n’ ‘e’

char[]

length: 7

char[] value

int hash

j.l.String

‘J’ ‘a’ ‘v’ ‘a’ ‘O’ ‘n’ ‘e’

char[]

length: 7

String conf = new String(“JavaOne”);

String j1 = new String(“JavaOne”);

String conf = new String(“JavaOne”);

String j1 = new String(“JavaOne”);

confconf

j1j1

✓✓

Page 42: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

String deduplication (JDK 8u20)

42

char[] value

int hash

j.l.String

‘J’ ‘a’ ‘v’ ‘a’ ‘O’ ‘n’ ‘e’

char[]

length: 7

char[] value

int hash

j.l.String

‘J’ ‘a’ ‘v’ ‘a’ ‘O’ ‘n’ ‘e’

char[]

length: 7

String conf = new String(“JavaOne”);

String j1 = new String(“JavaOne”);

String conf = new String(“JavaOne”);

String j1 = new String(“JavaOne”);

confconf

j1j1

✓✓private and final!

Page 43: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

String deduplication (JDK 8u20)

43

● During a collection G1 adds all newly allocated Strings to a queue

● After the young collection, G1 concurrently checks if any two Strings equals

● If two Strings are identical, make them refer to the same char[]

● Note: not the same as String.intern()● String.intern() cares about String objects, deduplication about char[]● Uses diferent tables internally in the JVM

Page 44: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

String deduplication (JDK 8u20)

44

● Can save a lot of memory depending on your application

● Trade-of: will use slightly more CPU

● Trade-of: might cause slightly longer young collections

● Try it out with -XX:+UseStringDeduplication in JDK 8u20 and later!

Page 45: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Five major improvements to G1 since JDK 8

1) String deduplication (JDK 8u20)

2) Class unloading with concurrent mark (JDK 8u40)

3) Eagerly reclaim humongous regions (JDK 8u60)

4) Adaptive start of concurrent mark (JDK 9)

5) More efficient collections (JDK 9)

45

Page 46: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Class unloading with concurrent mark (JDK 8u40)

46

CLCL

.class.class

.class.class

.class.class

Page 47: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Class unloading with concurrent mark (JDK 8u40)

47

CLCL

.class.class

.class.class

.class.class

Page 48: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Class unloading with concurrent mark (JDK 8u40)

48

CLCL

.class.class

.class.class

.class.class

Page 49: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Class unloading with concurrent mark (JDK 8u40)

49

CLCL

.class.class

.class.class

.class.class

Page 50: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Class unloading with concurrent mark (JDK 8u40)

50

JVM

Page 51: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Class unloading with concurrent mark (JDK 8u40)

51

JVM

Page 52: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Class unloading with concurrent mark (JDK 8u40)

52

JVM

Page 53: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Class unloading with concurrent mark (JDK 8u40)

53

JVM

Page 54: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Class unloading with concurrent mark (JDK 8u40)

54

● After all objects have been visited, thenunload all classes loaded by class loaders that

aren’t live

● Two algorithms in G1 visits all objects● Concurrent marking → tricky, but performant

● Fall-back full GC → easy, but slow

● In JDK 8u40 we shipped class unloading after concurrent marking

Page 55: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Class unloading with concurrent mark (JDK 8u40)

55

● Controlled by -XX:+ClassUnloadingWithConcurrentMark

● Enabled by default

● Reduces the need for fall-back full GCs

● Available in JDK 8u40 and later

Page 56: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Five major improvements to G1 since JDK 8

1) String deduplication (JDK 8u20)

2) Class unloading with concurrent mark (JDK 8u40)

3) Eagerly reclaim humongous regions (JDK 8u60)

4) Adaptive start of concurrent mark (JDK 9)

5) More efficient young collections (JDK 9)

56

Page 57: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Eagerly reclaim humongous regions (JDK 8u60)

57

● When objects are larger than half a region, they are called humongous

● Humongous objects are stored in humongous regions

● G1 doesn’t copy humongous regions, too expensive

● Want to collect as early as possible to free up memory

Humongous object

Page 58: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Eagerly reclaim humongous regions (JDK 8u60)

58

● G1 keeps tracks of references between old regions:

● So if a humongous region has zero incoming references…

● …then only an object in a young region can keep it alive

O O O O O H O O

Page 59: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Eagerly reclaim humongous regions (JDK 8u60)

59

● During a young collection, G1 checks references to humongous regions

● If no references are found, then the humongous object can be collected!

O O O O O H O O

Y Y Y Y Y Y Y Y

Page 60: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Eagerly reclaim humongous regions (JDK 8u60)

60

● G1 can therefore reclaim humongous objects during a young collection!

● G1 does not need to wait for concurrent mark to finish

● Introduced in JDK 8u60

Page 61: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Five major improvements to G1 since JDK 8

1) String deduplication (JDK 8u20)

2) Class unloading with concurrent mark (JDK 8u40)

3) Eagerly reclaim humongous regions (JDK 8u60)

4) Adaptive start of concurrent mark (JDK 9)

5) More efficient young collections (JDK 9)

61

Page 62: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Adaptive start of concurrent mark (JDK 9)

62

O

O

O

O

E

E

E

● Objects in old regions are mark concurrently

● Old regions can’t be collected until the concurrent mark finishes

Page 63: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Adaptive start of concurrent mark (JDK 9)

63

O

O

O

O

E

E

E

● Concurrent marking must finish before the heap is full with old regions

● When should it be started?

OOOOO

O

O

OO

O O O O

OE

Page 64: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Adaptive start of concurrent mark (JDK 9)

64

● Prior to JDK 9: -XX:InitiatingHeapOccupancyPercent (IHOP)

● Replaced in JDK 9 with an adaptive, dynamic calculation:

1) Use -XX:InitiatingHeapOccupancyPercent as initial value

2) Sample runtime data

3) Use data to make prediction

4) Always adds a safety margin!

Page 65: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Adaptive start of concurrent mark (JDK 9)

65

● Results in less concurrent mark failures → less fall-back full Gcs

● Controlled with the flag -XX:+G1UseAdaptiveIHOP

● Enabled by default

Page 66: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Five major improvements to G1 since JDK 8

1) String deduplication (JDK 8u20)

2) Class unloading with concurrent mark (JDK 8u40)

3) Eagerly reclaim humongous regions (JDK 8u60)

4) Adaptive start of concurrent mark (JDK 9)

5) More efficient collections (JDK 9)

66

Page 67: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

More efficient collections (JDK 9)

67

Impoved PLAB sizing

Parallel freeing of collection set

Parallel promotion failure

Improved work distribution Enable TLAB resizing

Remembered set space reduction

Ergonomic thread tuning

Concurrent mark from roots

Parallel pre-touch

More concurrent data structures

Decrease Hot Card Cache Contention

Parallel clear of the next bitmap

Array-based collection set

Improved concurrent refinement

Improved clearing of card table

Cache align and pad from the card cache

Page 68: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

More efficient collections (JDK 9)

68

Impoved PLAB sizing

Parallel freeing of collection set

Parallel promotion failure

Improved work distribution Enable TLAB resizing

Remembered set space reduction

Ergonomic thread tuning

Concurrent mark from roots

Parallel pre-touch

More concurrent data structures

Decrease Hot Card Cache Contention

Parallel clear of the next bitmap

Array-based collection set

Improved concurrent refinement

Improved clearing of card table

Cache align and pad from the card cache

250+enhancements

Page 69: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

More efficient collections (JDK 9)

69

Impoved PLAB sizing

Parallel freeing of collection set

Parallel promotion failure

Improved work distribution Enable TLAB resizing

Remembered set space reduction

Ergonomic thread tuning

Concurrent mark from roots

Parallel pre-touch

More concurrent data structures

Decrease Hot Card Cache Contention

Parallel clear of the next bitmap

Array-based collection set

Improved concurrent refinement

Improved clearing of card table

Cache align and pad from the card cache

180+bug fixes

Page 70: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

More efficient collections (JDK 9)

70

Impoved PLAB sizing

Parallel freeing of collection set

Parallel promotion failure

Improved work distribution Enable TLAB resizing

Remembered set space reduction

Ergonomic thread tuning

Concurrent mark from roots

Parallel pre-touch

More concurrent data structures

Decrease Hot Card Cache Contention

Parallel clear of the next bitmap

Array-based collection set

Improved concurrent refinement

Improved clearing of card table

Cache align and pad from the card cache

What is the effect?

Page 71: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

More efficient collections (JDK 9)

71

● G1 has been picked up by major Java frameworks and applications● Especially GUI applications and server-side frameworks

● Many major companies have switched or are switching to G1● Particularly for low latency workloads…

● … where throughput also is important!

● Always run the latest JDK release!

Page 72: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

The G1 Garbage Collector in JDK 9

Quick intro to G1 in 2017

Five major improvements since JDK 8

The future

1

2

3

72

Page 73: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

The future

73

● Great improvements for next JDK release● Parallel fall-back full GC - JEP 307● Faster card scanning

Page 74: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

The future

74

● Continue to push G1 further● Rebuild remembered sets concurrently● Improved ergonomics

Page 75: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Stay connected

75

• Email: [email protected]

– Many experienced users

– Also read by GC developers● might not always have time to answer…

• IM: #openjdk @ irc.oftc.net

Page 76: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.

Stay connected

• Join us: DevOps Corner (Developer Lounge – Moscone West)

• Learn more: openjdk.java.net | wercker.com/java

• Follow: @OpenJDK, @wercker #JavaOne #DevOps

76

Page 77: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK
Page 78: The G1 GC in JDK 9 - RainFocus G1 Garbage Collector in JDK 9 Quick intro to G1 in 2017 ... Class unloading with concurrent mark (JDK 8u40) 3) Eagerly reclaim humongous regions (JDK