JDK 9/10/11 and Garbage Collection Thomas Schatzl Senior Member of Technical Staf Oracle JVM Team May, 2018 thomas.schatzl@oracle.com Copyright 2017, Oracle and/or its afliates. All rights reserved. 1
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. 2
Agenda 1 The JDK and the JVM 2 Garbage Collector 3 Runtime 4 Compiler 5 Future 3
Agenda 1 The JDK and the JVM 2 Garbage Collector 3 Runtime 4 Compiler 5 Future 4
The Java Development Kit (JDK) Specifcction Tools Runtime Java Language javac JVM Java Virtual Machine jshell Java Class Library Java Platform jar jlink 5
The Java Development Kit (JDK) Specifcction Tools Runtime Jcvc Lcngucge javac JVM Java Virtual Machine jshell Java Class Library Java Platform jar jlink 6
The Java Development Kit (JDK) Specifcction Tools Runtime Java Language javac JVM Java Virtual Machine jshell Java Class Library Jcvc Plctform jar jlink 7
The Java Development Kit (JDK) Specifcction Tools Runtime Java Language javac JVM Java Virtual Machine jshell Java Class Library Java Platform jar jlink 8
The Java Development Kit (JDK) Specifcction Tools Runtime Java Language javac JVM Java Virtual Machine jshell Java Class Library Java Platform jar jlink 9
The Java Development Kit (JDK) Specifcction Tools Runtime Java Language javac JVM Java Virtual Machine jshell Java Class Library Java Platform jar jlink 10
The Java Virtual Machine (JVM) *.jar *.class App 11
The Java Virtual Machine (JVM) *.jar *.class Library Library *.jar App Library Library 12
The Java Virtual Machine (JVM) *.jar *.class Library Library *.jar App Library Library Java Class Library *.jmod 13
The Java Virtual Machine (JVM) *.jar *.class Library Library *.jar App Library Library Java Class Library *.jmod Java Virtual Machine libjvm.so 14
The Java Virtual Machine (JVM) *.jar *.class Library Library *.jar App Library Library Java Class Library *.jmod Java Virtual Machine libjvm.so GC Runtime Compiler 15
The Java Virtual Machine (JVM) *.jar *.class Library Library *.jar App Library Library Java Class Library *.jmod Java Virtual Machine libjvm.so GC Runtime Compiler 16
The Java Virtual Machine (JVM) Components Garbage collector Responsible for allocating memory and reclaiming unused memory Runtime Responsible for loading classes, operating system interaction and much more Compiler Responsible for compiling Java bytecode into highly efective machine code 17
Agenda 1 The JDK and the JVM 2 Garbage Collector 3 Runtime 4 Compiler 5 Future 18
Garbage Collector Responsible for allocating memory class Person { private String name; public Person(String name) { this.name = name; } } var p = new Person( John Doe ); 19
Garbage Collector Responsible for allocating memory class Person { private String name; public Person(String name) { this.name = name; } P e r s o n n a m e S t r i n g v a l u e 0 0 a r r a y 8 J o h n D o e } var p = new Person( John Doe ); Heap 20
Garbage Collector The heap is often very large 1 256 GB (even 1 TB) The heap contain billions of objects of diferent sizes of diferent types What happens when the heap is full? 21
Garbage Collector 1. Find live objects 22
Garbage Collector 1. Find live objects 2. Compact live objects 23
Garbage Collector Compaction can take a long time when the heap is very large the heap contain billions of live objects A Java application is often stopped during compaction Your application can be stopped for many seconds! not good for servers and GUI programs 24
Garbage Collector The G1 Garbage Collector G1 is the new default memory management algorithm since JDK 9 frst introduced in 6u14 Supported since 7u4 The goal: throughput and low latency The default pause goal for G1 is 200 milliseconds Higher pause goal more throughput, higher latency Lower pause goal less throughput, lower latency 25
Garbage Collector The G1 Garbage Collector G1 divides the heap into multiple regions 26
Garbage Collector The G1 Garbage Collector G1 divides the heap into multiple regions G1 fnds all live objects concurrently the Java application is not stopped 27
Garbage Collector The G1 Garbage Collector G1 divides the heap into multiple regions G1 fnds all live objects concurrently the Java application is not stopped 28
Garbage Collector The G1 Garbage Collector G1 divides the heap into multiple regions G1 fnds all live objects concurrently the Java application is not stopped 29
Garbage Collector The G1 Garbage Collector G1 divides the heap into multiple regions G1 fnds all live objects concurrently the Java application is not stopped G1 keeps track of pointers between regions 30
Garbage Collector The G1 Garbage Collector G1 divides the heap into multiple regions G1 fnds all live objects concurrently the Java application is not stopped G1 keeps track of pointers between regions G1 can therefore collect a few regions at a time fewer objects to collect shorter pauses more information more control over pauses 31
Garbage Collector The G1 Garbage Collector What happens if the heap becomes full before G1 fnds all live objects? frst, since JDK9 G1 tries to determine when to start this phase automatically so that the heap is not full fall back to secondary algorithm: full collection The full collection used to be single threaded can take a very long time on large heaps Solved in JDK 10! 32
Garbage Collector The G1 Garbage Collector JEP 307: Parallel Full GC For G1 http://openjdk.java.net/jeps/307 Parallel mark-sweep-compact algorithm Signifcantly improves G1 s worst case pause times Enabled by default Available in JDK 10 33
Garbage Collector The G1 Garbage Collector G1 in JDK 10 also features enhanced heap scanning Results in shorter pause times for young and mixed collections Results in less concurrent CPU usage Enabled by default Available in JDK 10 34
Agenda 1 The JDK and the JVM 2 Garbage Collector 3 Runtime 4 Compiler 5 Future 35
Runtime The runtime is responsibly for many parts: Loading Java classes Interacting with the operating system Concurrency primitives (threads, monitors, etc.) Logging Diagnostics Serviceability 36
Runtime The runtime is responsibly for many parts: Loading Java classes Interacting with the operating system Concurrency primitives (threads, monitors, etc.) Logging Diagnostics Serviceability 37
Runtime Loading Java classes DefineClass() javac App.java (text flee App.class (binary flee Klass* (in-memory objecte 38
Runtime Loading Java classes Loading a Java class from a.class fle takes time must load.class content from disk must verify class fle must create JVM in-memory representation must resolve and link references Many times you re-start the same Java program all this work must be repeated every time the JVM starts Can this be optimized? 39
Runtime Loading Java classes JDK 1.5 shipped with Class-Data Sharing (CDS) CDS stores already loaded and verifed classes to a shared archive fle can be quickly loaded by the JVM at startup can also be shared by multiple JVMs Limitations only for the client JVM only for the Serial garbage collector only works with system classes 40
Runtime Loading Java classes JDK 9 shipped with enhanced Class-Data Sharing works with the G1, Parallel and Serial garbage collectors works with both client and server JVMs works with shared strings (only with the G1 GC) Limitations still only applicable to system classes 41
Runtime Loading Java classes JDK 9 also shipped Application Class Data Sharing (AppCDS) enables application classes to also be stored to in the shared archive fle, e.g. Person Commercial feature Only available in Oracle JDK 9 42
Runtime Loading Java classes AppCDS was open sourced for JDK 10! JEP 310: Application Class-Data Sharing http://openjdk.java.net/jeps/310 Results in even faster startup times great for spinning up new JVMs on demand quickly 43
Runtime Loading Java classes 1) Find out which classes to share: $ java -Xshare:off -XX:+UseAppCDS -XX:DumpLoadedClassList=app.lst \ -cp lib.jar App 2) Create the shared archive fle: $ java -Xshare:dump -XX:+UseAppCDS \ -XX:SharedClassListFile=app.lst \ -XX:SharedArchiveFile=app.jsa \ -cp lib.jar 3) Use the shared archive fle: $ java -Xshare:auto -XX:+UseAppCDS -XX:SharedArchiveFile=app.jsa \ -cp lib.jar App 44
Runtime Interacting with the operating system The runtime queries the operating system (OS) for determining active CPUs how much memory is available other resource limits Important to use the right APIs in a world of containers do not query the CPU directly must be aware of Linux namespaces 45
Runtime Interacting with the operating system The JVM in JDK 10 uses the correct OS interfaces Uses cgroups to fnd memory limits Default heap size will no longer be way too big Uses cpuset to limit number of cores Default number of GC and compiler threads will be correct Uses the pid namespace for the attach API JDK tools such as container jmap can connect to a JVM running in a 46
Runtime Interacting with the operating system New more intuitive fags for setting heap sizing -XX:MinRAMPercentage -XX:InitialRAMPercentage -XX:MaxRAMPercentage Works great in combination with containers no need to update JVM fags when changing container memory limits 47
Runtime Interacting with the operating system JDK 10 now much better supports containers AppCDS enables faster startup Using the correct OS interfaces enables better defaults better tooling New fags enables more intuitive heap sizing Make sure to upgrade to JDK 10 if you are using Docker! 48
Agenda 1 The JDK and the JVM 2 Garbage Collector 3 Runtime 4 Compiler 5 Future 49
Compiler Bytecode Interpreter C1 C2 JIT JIT 50
Compiler Improved ahead of time compiler JDK 9 introduced the ahead-of-time (AOT) compiler jaotc javac Java source code (texte jaotc Java.class fles (binarye Shared library (machine codee 51
Compiler Improved ahead of time compiler JDK 9 introduced the ahead-of-time (AOT) compiler jaotc Shared library (machine codee C1 C2 JIT JIT 52
Compiler Improved ahead of time compiler JDK 9 introduced the ahead-of-time (AOT) compiler jaotc Limitations did not support all bytecodes only worked on Linux experimental support 53
Compiler Improved ahead of time compiler JDK 10 features an enhanced AOT compiler now supports the invokedynamic bytecode works on Linux, macos and Windows Still experimental support Use with $ jaotc --output libapp.so App.class 54
Compiler Experimental Java-Based JIT Compiler JDK 10 also ships with a new JIT compiler: Graal C1 Bytecode Interpreter JIT Graal (JITe 55
Compiler Experimental Java-Based JIT Compiler JDK 10 also ships with a new JIT compiler: Graal Only support for Linux Only experimental support May or may not make your application faster Use with $ java -XX:+UnlockExperimentalVMOptions \ -XX:+UseJVMCICompiler 56
Agenda 1 The JDK and the JVM 2 Garbage Collector 3 Runtime 4 Compiler 5 Future 57
Future JDK 11 JDK 11 scheduled for release in September, 2018 Dynamic Class-File Constants (JEP 309) Local Variable Syntax for Lambda Parameters (JEP 323) Flight Recorder (JEP 328) Runtime: Improved Metaspace allocation Compiler: Build Graal on Windows and Mac 58
Future JDK 11 JDK 11 scheduled for release in September, 2018 G1 GC Dynamic Number of Thread Sizing Reduced Native Memory Footprint Signifcantly Lower Pause Times Improved Throughput 59
Future JDK 11 http://jdk.java.net/11 Now is the right time to provide feedback! 60
Future Projects Many exciting ongoing projects ZGC Portola Valhalla Loom Panama See http://openjdk.java.net for more projects and information 61
Future Projects Many exciting ongoing projects ZGC Portolc Valhalla Loom Panama See http://openjdk.java.net for more projects and information 62
Future Project ZGC ZGC is a new scalable low latency garbage collector pause times below 10 ms pause times do not increase with heap size (nor live-set size) built for heaps from a few gigabytes to multiple terabytes in size Finds all live objects concurrently Compacts live objects concurrently 63
Future Project ZGC http://jdk.java.net/zgc https://wiki.openjdk.java.net/display/zgc/main 64
Future Project Portola Port of OpenJDK to Alpine Linux Alpine Linux is a small and lightweight Linux distribution Uses the musl C standard library Size of Alpine Linux when used as Docker base image: 4 MB compare to Ubuntu: 120MB Use jlink to produce a small Java Runtime Environment (JRE) a Docker image for java.base and Alpine Linux is 40 MB 65
Future Project Portola http://jdk.java.net/11 http://openjdk.java.net/projects/portola/ 66
Agenda 1 The Java Virtual Machine 2 Garbage Collector 3 Runtime 4 Compiler 5 Future 67
Summary JDK 9+ contain great improvements to the JVM G1 as default collector Parallel Full GC Reduced Pause times Improved Throughput AppCDS Better container support Improved AOT Experimental Graal support 68
Summary http://jdk.java.net/10 http://jdk.java.net/11 Please report issues to: https://bugreport.java.com/ GC experience reports/troubleshooting e-mail hotspot_gc_use@openjdk.java.net 69
Questions? 70
Thanks go to... Erik Duveblad For a large amount of the slides >100 Developers from many companies that contribute to OpenJDK You for supporting and improving our work with your feedback 72