Copyright © 2019, Oracle and/or its affiliates. All rights reserved.
Oleg Šelajev
GraalVM team, Oracle Labs
@shelajev
Native Images
Copyright © 2019, Oracle and/or its affiliates. All rights reserved.
Safe Harbor Statement
The following is intended to provide some insight into a line of research in Oracle
Labs. 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 in
connection with any Oracle product or service remains at the sole discretion of
Oracle. Any views expressed in this presentation are my own and do not
necessarily reflect the views of Oracle.
2
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !3
Seeing is believing
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | !4
Fast Java, Scala, Kotlin, Groovy, Clojure...
Instant startup, low footprint
Polyglot & embeddable
Interoperability between languages: node.js, Python, Ruby, R
Why GraalVM?
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | !5
Fast Java, Scala, Kotlin, Groovy, Clojure...
Instant startup, low footprint
Polyglot & embeddable
Interoperability between languages: node.js, Python, Ruby, R
Why GraalVM?
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !6
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !7
GraalVM native images
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !8
Native images
• Full AOT compilation to machine code
• Works with memory management
• Secure execution (e.g., bounds checks)
• Embeddable with native applications
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !9
Native images
• Full AOT compilation to machine code
• Works with memory management
• Secure execution (e.g., bounds checks)
• Embeddable with native applications
Copyright © 2019, Oracle and/or its affiliates. All rights reserved.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !11
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | !12
Image build time Runtime
Native image lifecycle
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !13
Static initializers
Static class initialization blocks, pre-initialized static variables.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !14
Static initializers
By default static class initialization is done
during native image construction.
Large static data structures are pre-allocated
allowing faster startup of the generated image.
No instance-specific initializations can be done in static
initializers.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !15
Static initializers - things not to do
• start application threads that continue to run in the background
• load native libraries using `java.lang.Runtime.load(String)`
• open files or sockets, or
• allocate C memory, e.g., `java.nio.ByteBuffer.allocateDirect(int)`.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !16
Static initializers
Write your own initialization methods
and call them explicitly from your main entry point.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !17
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !18
> $JAVA_HOME/bin/javac HelloStartupTime.java
> $JAVA_HOME/bin/java HelloStartupTime
Startup: Fri Aug 31 13:17:05 PDT 2018
Now: Fri Aug 31 13:17:05 PDT 2018
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !19
> $JAVA_HOME/bin/javac HelloStartupTime.java
> $JAVA_HOME/bin/java HelloStartupTime
Startup: Fri Aug 31 13:17:05 PDT 2018
Now: Fri Aug 31 13:17:05 PDT 2018
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !20
> $JAVA_HOME/bin/javac HelloStartupTime.java
> $JAVA_HOME/bin/java HelloStartupTime
Startup: Fri Aug 31 13:17:05 PDT 2018
Now: Fri Aug 31 13:17:05 PDT 2018
> $JAVA_HOME/bin/native-image HelloStartupTime
> ./hellostartuptime
Startup: Fri Aug 31 13:22:12 PDT 2018
Now: Fri Aug 31 14:35:42 PDT 2018
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !21
Static initializers config
--initalize-at-build-time=package
--initialize-at-run-time=p.C1
-H:+PrintClassInitialization
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !22
Reflection
-H:ReflectionConfigurationFiles=
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !23
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !24
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !25
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !26
Resources
-H:IncludeResources=<regexp>
-H:IncludeResources=
"application.yml|META-INF/services/*.*"
Copyright © 2019, Oracle and/or its affiliates. All rights reserved.
-H:+ReportUnsupportedElementsAtRuntime
--allow-incomplete-classpath
!27
java.lang.NoClassDefFoundError
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !28
Substitutions
Core classes (annotations):
com.oracle.svm.core.annotate.TargetClass
com.oracle.svm.core.annotate.Substitute
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !29
Image build time vs runtime
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !30
native-image.properties
META-INF/native-image/
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !31
native-image.properties
META-INF/native-image/groupId/artifactId/
reflection-config.json
META-INF/native-image/groupId/artifactId/
native-image.properties
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !32
native-image.properties
ImageName = netty-server
Args = --features=com.oracle.svm.nettyplot.PlotterSingletonFeature 
       -H:ReflectionConfigurationResources=${.}/reflection-config.json 
       --delay-class-initialization-to-runtime=
io.netty.handler.codec.http.HttpObjectEncoder 
       -H:+SpawnIsolates
Copyright © 2019, Oracle and/or its affiliates. All rights reserved.
https://medium.com/graalvm/instant-netty-startup-using-graalvm-native-
image-generation-ed6f14ff7692
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !34
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 35
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 36
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !37
https://github.com/spring-projects/spring-framework/wiki/GraalVM-native-image-support
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | !38
Work in progress
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !39
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !40
GC
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !41
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !42
Heapdump
-H:+AllowVMInspection
$ ps -e | grep native-name
$ kill -SIGUSR1 <pid>
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !43
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !44
Runtime
https://github.com/giltene/wrk2
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !45
Runtime
https://github.com/giltene/wrk2
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !46
https://github.com/PPACI/wrk2img
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !47
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !48
High performance, polyglot, language-level virtualization layer…
embeddable across the stack
in native and JVM-based applications.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. !49

GraalVM Native Images by Oleg Selajev @shelajev

  • 1.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. Oleg Šelajev GraalVM team, Oracle Labs @shelajev Native Images
  • 2.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. Safe Harbor Statement The following is intended to provide some insight into a line of research in Oracle Labs. 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 in connection with any Oracle product or service remains at the sole discretion of Oracle. Any views expressed in this presentation are my own and do not necessarily reflect the views of Oracle. 2
  • 3.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !3 Seeing is believing
  • 4.
    Copyright © 2018,Oracle and/or its affiliates. All rights reserved. | !4 Fast Java, Scala, Kotlin, Groovy, Clojure... Instant startup, low footprint Polyglot & embeddable Interoperability between languages: node.js, Python, Ruby, R Why GraalVM?
  • 5.
    Copyright © 2018,Oracle and/or its affiliates. All rights reserved. | !5 Fast Java, Scala, Kotlin, Groovy, Clojure... Instant startup, low footprint Polyglot & embeddable Interoperability between languages: node.js, Python, Ruby, R Why GraalVM?
  • 6.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !6
  • 7.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !7 GraalVM native images
  • 8.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !8 Native images • Full AOT compilation to machine code • Works with memory management • Secure execution (e.g., bounds checks) • Embeddable with native applications
  • 9.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !9 Native images • Full AOT compilation to machine code • Works with memory management • Secure execution (e.g., bounds checks) • Embeddable with native applications
  • 10.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved.
  • 11.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !11
  • 12.
    Copyright © 2018,Oracle and/or its affiliates. All rights reserved. | !12 Image build time Runtime Native image lifecycle
  • 13.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !13 Static initializers Static class initialization blocks, pre-initialized static variables.
  • 14.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !14 Static initializers By default static class initialization is done during native image construction. Large static data structures are pre-allocated allowing faster startup of the generated image. No instance-specific initializations can be done in static initializers.
  • 15.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !15 Static initializers - things not to do • start application threads that continue to run in the background • load native libraries using `java.lang.Runtime.load(String)` • open files or sockets, or • allocate C memory, e.g., `java.nio.ByteBuffer.allocateDirect(int)`.
  • 16.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !16 Static initializers Write your own initialization methods and call them explicitly from your main entry point.
  • 17.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !17
  • 18.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !18 > $JAVA_HOME/bin/javac HelloStartupTime.java > $JAVA_HOME/bin/java HelloStartupTime Startup: Fri Aug 31 13:17:05 PDT 2018 Now: Fri Aug 31 13:17:05 PDT 2018
  • 19.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !19 > $JAVA_HOME/bin/javac HelloStartupTime.java > $JAVA_HOME/bin/java HelloStartupTime Startup: Fri Aug 31 13:17:05 PDT 2018 Now: Fri Aug 31 13:17:05 PDT 2018
  • 20.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !20 > $JAVA_HOME/bin/javac HelloStartupTime.java > $JAVA_HOME/bin/java HelloStartupTime Startup: Fri Aug 31 13:17:05 PDT 2018 Now: Fri Aug 31 13:17:05 PDT 2018 > $JAVA_HOME/bin/native-image HelloStartupTime > ./hellostartuptime Startup: Fri Aug 31 13:22:12 PDT 2018 Now: Fri Aug 31 14:35:42 PDT 2018
  • 21.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !21 Static initializers config --initalize-at-build-time=package --initialize-at-run-time=p.C1 -H:+PrintClassInitialization
  • 22.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !22 Reflection -H:ReflectionConfigurationFiles=
  • 23.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !23
  • 24.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !24
  • 25.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !25
  • 26.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !26 Resources -H:IncludeResources=<regexp> -H:IncludeResources= "application.yml|META-INF/services/*.*"
  • 27.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. -H:+ReportUnsupportedElementsAtRuntime --allow-incomplete-classpath !27 java.lang.NoClassDefFoundError
  • 28.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !28 Substitutions Core classes (annotations): com.oracle.svm.core.annotate.TargetClass com.oracle.svm.core.annotate.Substitute
  • 29.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !29 Image build time vs runtime
  • 30.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !30 native-image.properties META-INF/native-image/
  • 31.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !31 native-image.properties META-INF/native-image/groupId/artifactId/ reflection-config.json META-INF/native-image/groupId/artifactId/ native-image.properties
  • 32.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !32 native-image.properties ImageName = netty-server Args = --features=com.oracle.svm.nettyplot.PlotterSingletonFeature        -H:ReflectionConfigurationResources=${.}/reflection-config.json        --delay-class-initialization-to-runtime= io.netty.handler.codec.http.HttpObjectEncoder        -H:+SpawnIsolates
  • 33.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. https://medium.com/graalvm/instant-netty-startup-using-graalvm-native- image-generation-ed6f14ff7692
  • 34.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !34
  • 35.
    Copyright © 2016,Oracle and/or its affiliates. All rights reserved. | 35
  • 36.
    Copyright © 2016,Oracle and/or its affiliates. All rights reserved. | 36
  • 37.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !37 https://github.com/spring-projects/spring-framework/wiki/GraalVM-native-image-support
  • 38.
    Copyright © 2018,Oracle and/or its affiliates. All rights reserved. | !38 Work in progress
  • 39.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !39
  • 40.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !40 GC
  • 41.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !41
  • 42.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !42 Heapdump -H:+AllowVMInspection $ ps -e | grep native-name $ kill -SIGUSR1 <pid>
  • 43.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !43
  • 44.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !44 Runtime https://github.com/giltene/wrk2
  • 45.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !45 Runtime https://github.com/giltene/wrk2
  • 46.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !46 https://github.com/PPACI/wrk2img
  • 47.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !47
  • 48.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !48 High performance, polyglot, language-level virtualization layer… embeddable across the stack in native and JVM-based applications.
  • 49.
    Copyright © 2019,Oracle and/or its affiliates. All rights reserved. !49