Understanding Java Virtual Machine - Bangalore...

54

Transcript of Understanding Java Virtual Machine - Bangalore...

Page 1: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual
Page 2: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Understanding Java Virtual Machine

Vaibhav Choudhary (@vaibhav_c) Java Platforms Team, Oracle https://blogs.oracle.com/vaibhav

Page 3: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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.

3

Page 4: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Agenda of the day …

Java Virtual Machine (JVM) - Architectural view

Working through ClassLoaders

Understanding optimizations of Runtime Compilers

Automated Memory Management - Garbage Collectors

Final Thoughts

1

2

3

4

5

4

Page 5: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Java Virtual Machine - Architectural View

.class file Java ClassLoader Sub-system

Runtime Data AreaHeap Stack Native Methods

Method Area

Java Execution Engine GC JIT Native Method

InterfaceNative Method

LibrariesCodeCache

Page 6: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Beginning of Java ClassLoader Sub-system

Page 7: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Class And Data• Class represents code. Data represents state. • State often change, code mostly not. • Class + State = Instance • Once a class is loaded in JVM, the same class (?) will not be loaded. • Same class concept • (C1, P1, K1) != (C1, P1, K2)

7

Page 8: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Class loader types• Bootstrap class loader (primordial class loader) • Special class loader • VM runs without verification • Access to the repository of “trusted classes” • Native implementation • <JAVA_HOME>/jre/lib (rt.jar, i18n.jar)

8

Page 9: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Class loader types• Extension class loader • Loads the code in the extension directories • <JAVA_HOME>/jre/lib/ext • -Djava.ext.dir • Java implementation - sun.misc.Launcher$ExtClassLoader

9

Page 10: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Class loader types• Application class loader • Loads the code found in java.class.path which maps to

CLASSPATH. • Java implementation - sun.misc.Launcher$AppClassLoader

10

Page 11: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Class loader parent delegation model

11

GET

ME

THIS

CLA

SS{ N

ame,

Res

olve

}

Application Classloader

Extension Classloader

Bootstrap Classloader

CLA

SSLO

ADER

SU

B-SY

STEM

MO

DELDelegate to

Delegate to

Runtime Libraries

Extension Libraries

Look into

!found, return null

!found, return null

Look into

!found, return nullDelegate to

Classpath Libraries Look into

if nullClassNotFoundException

Delegate to

Page 12: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

protected synchronized Class<?> loadClass (String name, boolean resolved) throws ClassNotFoundException {

// First check if the class is already loaded Class c = findLoadedClass(name); if (c == null) { try { if (parent != null) { c = parent.loadClass(name, false); // parent delegation } else {

c = findBootstrapClass0(name); } } catch (ClassNotFoundException e) { c = findClass(name); // if still not found, invoke findClass } } if(resolved) { resolveClass(c); } return c;}

In short, this is how it works

Page 13: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Parallel Class loading• JDK7 made an important enhancement to Multi-threading custom

class loader. • Previously, certain class loader were prone to deadlock. • JDK7 modifies the lock mechanism. • Previously as well, it was not an issue which adhere the acyclic

delegation model. • http://openjdk.java.net/groups/core-libs/

ClassLoaderProposal.html • protected static boolean registerAsParallelCapable()

Page 14: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Deadlock Scenario

Class Hierarchy: class A extends B class C extends D

ClassLoader Delegation Hierarchy:

Custom Classloader CL1: directly loads class A delegates to custom ClassLoader CL2 for class B

Custom Classloader CL2: directly loads class C delegates to custom ClassLoader CL1 for class D

Thread 1: Use CL1 to load class A (locks CL1) defineClass A triggers loadClass B (try to lock CL2)

Thread 2: Use CL2 to load class C (locks CL2) defineClass C triggers loadClass D (try to lock CL1)

Page 15: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

JDK7 - Resolution

Class Hierarchy: class A extends B class C extends D

ClassLoader Delegation Hierarchy:

Custom Classloader CL1: directly loads class A delegates to custom ClassLoader CL2 for class B

Custom Classloader CL2: directly loads class C delegates to custom ClassLoader CL1 for class D

Thread 1: <if parallel capable> Use CL1 to load class A (locks CL1 + A) defineClass A triggers loadClass B (try to lock CL2 + B)

Thread 2: <if parallel capable> Use CL2 to load class C (locks CL2 + C) defineClass C triggers loadClass D (try to lock CL1 + D)

Page 16: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

End of Java ClassLoader Sub-system

Page 17: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Beginning of Java Runtime Compilers (JIT)

Page 18: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Why we need Runtime Compilers ?• Interpreter are slow in nature. How much ? • We always have scope of optimization of runtime code. • Can we be biased towards a path which is 99.9 percent times right ? • With more run, we get profiled data and optimizations can be done

as per the information. • Just-In-Time (JIT) Compiler comes with 2 flavors to address all

these :- • C1 - Less optimized, fast startup - perfect for UI (Client) app. • C2 - More optimized, slow startup - perfect for Server app.

Page 19: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

In working, where I see JIT ?• Whenever you use any of these flags :-

• -client [C1] • -server [C2] • -XX:+TieredCompilation [Mix of C1, C2] • -Xint [Only interpreter] • -Xcomp [Only compiler] • -Xmixed [Mix of interpreter and compiler]

Page 20: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Optimizations_1 (Dead code removal + Expression Opt)

void someCalculation(int x1, int x2, int x3) {   int res1 = x1+x2; int res2 = x1-x2;   int res3 = x1+x3;

(res1+res2)/2; }

void someCalculation(int x1, int x2, int x3) {   x1;

}

Page 21: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Optimizations_2 (Monomorphic Dispatch)

Animal a = new Animal(); a.color;

public class Animal { private String color;

public String getColor() { return color; } }

Animal a = new Animal(); a.getColor();

If not overridden

Page 22: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Optimizations_3 (Null Check Optimization)• How Java handles Null Pointer ?

Point point = new Point(); x = point.x; y = point.y;

Point point = new Point(); if(p==null) throw NPE;

else { x = point.x; y = point.y;

}

runtimeconversion

• if profiled information says that, p is never going for null • remove the if-else condition • insert UnCommonTrap [ path to de-optimization]

Page 23: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

New VM Based Languages (Kotlin Example)

fun main(args : Array<String>) { var a: String = "abc" a = null // compilation error

var b: String? = "abc" b = null // ok

val l = a.length //ok

val l = b.length // error: variable 'b' can be null val l = if (b != null) b.length else -1 }

Page 24: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Optimizations_3_4 (Inlining and Caching)• Inlining is one of the most powerful optimization. Method call

replaced by the method body if function is pure. • Inline is method is < 35 KB • Inlining in Java is more powerful than languages like C++

• Caching is also a good optimization technique • p.x = (p1.x + p2.x) / 2 ; p.y = (p1.y + p2.y) / 2

Page 25: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Optimizations_5_6 (Thread and Loop Optimization)• Eliminate lock if not reachable from other thread. • Join sync blocks on the same object, if possible. • BaisedLocking

• Loop combining - if possible • Replacing while from do-while • Tiling Loops - Fitting into the cache exactly

Page 26: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Real life challenges and Future• Higher start-up time - Not a good idea in low latency systems. • Different techniques.

• Future may be better (AOT, AppCDS …)

Page 27: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

End of Java Runtime Compilers (JIT)

Page 28: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Beginning of Java Garbage Collectors

Page 29: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Stack and Heap in Java

29

Stack

Heap

public static void main(String[] args) {

Object obj = new Object(); String str = new String(); int count = 0; // Do some work here callNext();

}

public void callNext() {

List list = new ArrayList(); // some work

}

Stack

main()objstr

count=0

callNext()list

Heap

String Poolstr_object

obj

list

Page 30: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Initial Thoughts • Reference Count for all the “Object” Instance. • If refCount == 0, should be eligible for GC.

Object1 (1)

Object2(2)

Object3(1)

Object4(2)

Object1 (1)

Object2(2)

Object3(0)

Object4(2)

X

Page 31: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Initial Thoughts • But tend to create “Achilles Heel” • If refCount != 0, should be eligible for GC ?

Object1 (2)

Object2(1)

Object3(1)

Object1 (1)

Object2(1)

Object3(1)X

Page 32: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Definition• Mark

➤ find and mark all accessible objects

• Sweep ➤ scans through the heap and reclaims all the unmarked objects

• Copy ➤ copies all live objects from one part of the heap to another empty part

• Compaction ➤ moving all the live objects into contiguous memory locations

32

Page 33: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Java Heap Structure

33

Eden Generation

S0 S1 Old Generation Meta Space

Minor GC Major GC

new Object() After minor GC After threshold limit (n)

Weak Generational Hypothesis : - Most of the object die young. - There are very few old to young reference.

Page 34: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Parallel And Concurrent

Application Thread

in Parallel

GC Thread

in Parallel

Application and GCThread

in Concurrent Mode

Page 35: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

CMS Simplified (In Young generation)…

35

Old generation

Eden

S1S0

Old generation

S1S0Eden

Old generation

Eden

S1S0

When eden space will be full. Minor GC will be triggered. Usages of S0 and S1.

Page 36: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

CMS Simplified (In Young generation)…

36

Old generation

Eden

S1S0

Old generation

S1S0Eden

Old generation

Eden

S1S0 S1

Empty Eden and one of the Survivors after Minor GC completion. Stop the world process. Some objects can be promoted to Old generation.

Page 37: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

CMS Simplified (In Old generation)…

37

No compaction in CMS Mostly concurrent

Old generation will mostly look like thisMay be the first snapshot of Old generation

Page 38: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

CMS Simplified (In Old generation)…

38

CMS phases :-

1. Initial marking phase. 2. Concurrent Marking phase. 3. Concurrent Pre-cleaning Phase. 4. Remarking phase. 5. Concurrent Sweep Phase. 6. Concurrent Reset Phase.

STW

STW

Page 39: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

CMS Simplified (In Old generation)…

39

Initial Marking Phase :-

1. Scan the object directly reachable from root or from Eden Generation.

2. All the mutator thread need to be stopped (STW).

3. STW - We don’t want to mess up with this information.

4. Generally small pause in nature.

G C

R O O T

Eden Generation

Page 40: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

CMS Simplified (In Old generation)…

40

Concurrent Marking Phase :-

1. Scan almost all live objects from the object marked from phase 1.

2. This will be done in concurrent fashion.

3. And this will lead to a remarking phase.

4. It will take time but it’s concurrent.

G C

R O O T

Eden Generation

Page 41: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

CMS Simplified (In Old generation)…

41

Remarking Phase :-

1.Scan the newly created objects.

2. We need to do STW.

3. Generally, small pause in nature.

G C

R O O T

Eden Generation

We end up creating new Objects, because last phase is concurrent in nature.

Page 42: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

CMS Simplified (In Old generation)…

42

Remarking Phase :-

1.Scan the newly created objects.

2. We need to do STW.

3. Generally, small pause in nature.

G C

R O O T

Eden Generation

We end up creating new Objects, because last phase is concurrent in nature.

Page 43: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

CMS Simplified (In Old generation)…

43

Concurrent Sweep Phase :-

1. Start sweeping all the non-marked Object.

2. Concurrent in nature.

3. Red boxes can be claimed by GC.

G C

R O O T

Eden Generation

We end up creating new Objects, because last phase is concurrent in nature.

Page 44: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Final thoughts for CMS• It’s almost concurrent other than some small STW’s. • Memory Fragmentation - so can’t fit large object. • Less predictable. • Can Lead to promotion failure - Young to old promotion is not

happening because object is too big or old space is almost full. • Can Lead to Concurrent mode failure - Old generation not finished

the collection work and Old generation is full. STW and then run a different GC algorithm, probably Mark-Sweep-Compact. • If application is working fine with CMS, let it work !

44

Page 45: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Welcome to Garbage First (G1)• Default GC algorithm for JDK9. • G1 Goals – Low latency

– Predictable (Can’t be 100 percent)

– Easy to use (Less parameter settings)

•Concurrent, Parallel and better Compacting. • If you are not on JDK9, use -XX:+UseG1GC. •Careful with your greedy throughput desires.

45

Page 46: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Garbage First (G1) - Memory layout

46

O Y

- Memory is divided into small regions - More than 2000 regions - More flexible boundaries - Use -XX:+G1HeapRegionSize - Different regions :

- Young - Survivor - Old - Humongous

S

H H H

YY

Y

Y

Y

O

O

O

O

O

O O

S

Page 47: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

G1 - Young region View

47

O1

O2 O3

O1

O2 O3

GC Root

O4None of the object is reachable from GC root in first block. Looks like the complete region can be collected. (No you can’t)

GC root is pointing to some object. Can’t collect this region.

Page 48: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

G1 - Young region View

48

O1

O2 O3

O1

O2 O3

GC Root

O4O1

O2 O3

O4We have reference from Old region to this block. Can’t be collected.

Remembered Set (Remembers the references from old region)

Page 49: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

G1 - Ease to use

49

• java -Xmx50m -XX:+UseG1GC -XX:+MaxGCPauseMillis=200 -jar Java2DDemo.jar • MaxGCPauseMillis=200 - A soft goal. G1 will try to

respect as much as possible, but can’t guarantee you. • Important to know – -XX:+InitiatingHeapOccupancyPercentage=45

Page 50: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

G1 - Young GC View

O Y

S

H H H

YY

Y

Y

Y

O

O

O

O

O

O O

S

Page 51: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

G1 - Young GC View

O

H H H

Y

Y

O

O

O

O

O

O O

S

O

Page 52: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

G1 - Best Practices

52

• Avoid setting up too much of parameter. – You will end up G1 behaving abruptly.

• Avoid full GC. Look at the adaptive policy. • Avoid allocation Failure – Very similar to Concurrent Mark Failure in CMS.

– It will call STW and a full GC

– Issue with heap or with the code

• Careful with Humongous Regions. Change region size, if it is more.

Page 53: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

End of Java Garbage Collectors

Page 54: Understanding Java Virtual Machine - Bangalore JUGbangalorejug.org/.../Understanding-Java-Virtual-Machine.pdf · 2017-08-19 · Java Virtual Machine. Title: Understanding Java Virtual

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

Understood Java Virtual Machine

Vaibhav Choudhary (@vaibhav_c) Java Platforms Team, Oracle https://blogs.oracle.com/vaibhav

For any query, mail at [email protected]