GraalVM is a universal virtual machine that allows applications written in Java and other languages like JavaScript, Python, Ruby, and R to be compiled into self-contained binaries. This improves performance by reducing startup time and memory usage compared to the traditional JVM. The main features of GraalVM include a modern compiler that converts bytecode into native images, polyglot interoperability between multiple languages, and the ability to implement custom languages. While Scala Native is a competitor that focuses on optimizing Scala code ahead of time, GraalVM supports a wider range of languages and has a larger community behind it.
2. Agenda
• Developing for the JVM
• Main GraalVM features
• GraalVM in practice
• GraalVM vs Scala Native
• Demo
2
3. DevelopingfortheJVM
• Write once, run anywhere
• The Java Virtual Machine is an execution engine for Java bytecode
• Write once, debug anywhere
• Large memory footprint, slow startup time, slow execution
• Exacerbated with the shift to modern computation & deployment standards
• Microservices, containers: higher in number, smaller in size …
• Cloud computing: transient resources …
Joys and sorrows
3
4. Johannes Brüderl – source
“While it’s certainly bearable to have 100MB+
large hello world applications in 2018, it’s just
weak if I can have a 6MB Go binary”
4
6. Renato Athaydes – source
“A 7MB native-image Java app that runs in
30ms and uses only 4MB of RAM!”
6
7. MainGraalVMfeatures
• Modern compiler, turning JVM applications into self-contained binaries
• Drop JRE -> fast startup, low memory footprint
• native-image compiler, taking bytecode as input
• Polyglot applications: interoperability of multiple languages
• Universal Virtual Machine: JavaScript, Python, Ruby, C, R out of the box
Why should we care
7
8. const express = require('express')
const app = express()
const BigInteger = Java.type('java.math.BigInteger')
app.get('/', function (req, res) {
var text = 'Hello World from Graal.js!<br> '
// Using Java standard library classes
text += BigInteger.valueOf(10).pow(100)
.add(BigInteger.valueOf(43)).toString() + '<br>'
// Using R interoperability to create graphs
text += Polyglot.eval('R',
`svg();
require(lattice);
x <- 1:100
y <- sin(x/10)
z <- cos(x^1.3/(runif(1)*5+10))
print(cloud(x~y*z, main="cloud plot"))
grDevices:::svg.off()
`);
res.send(text)
})
8
9. 9
const express = require('express')
const app = express()
app.get('/', function (req, res) {
var text = 'Hello World from Graal.js!<br> '
// Using Java standard library classes
text +=
// Using R interoperability to create graphs
text += Polyglot.eval('R',
`
`);
res.send(text)
})
Java
JavaScript
svg();
require(lattice);
x <- 1:100
y <- sin(x/10)
z <- cos(x^1.3/(runif(1)*5+10))
print(cloud(x~y*z, main="cloud plot"))
grDevices:::svg.off()
const BigInteger = Java.type('java.math.BigInteger')
BigInteger.valueOf(10).pow(100)
.add(BigInteger.valueOf(43)).toString() + '<br>'
R
10. MainGraalVMfeatures
• Modern compiler, turning JVM applications into self-contained binaries
• Drop JRE -> fast startup, low memory footprint
• native-image compiler, taking bytecode as input
• Polyglot applications: interoperability of multiple languages
• Universal Virtual Machine: JavaScript, Python, Ruby, C, R out of the box
• Implement your own language
• Truffle Language Implementation Framework
• Write once, compile anywhere
Why should we care
10
11. GraalVMinpractice
• Ahead-Of-Time (AOT) compilation
• Performance trade-off: moves the complexity / overhead from the runtime to the
compilation
• Closed-world analysis of all the bytecode that will be executed at run-time
• Some limitations:
• Incompatible with dynamically created bytecode – e.g. reflection, dynamic class
loading, metaprogramming
• Other interpreted languages are not compiled
Native Image
11
12. Technicaldetails
• Based on Oracle’s HotSpot
• Currently supports Java 8 + Java 11
• GraalVM Community vs GraalVM Enterprise
• Enterprise recommended for production applications
• native-image -jar target/test.jar
• sbt graalvm-native-image:packageBin
• Fallback AOT-compiled images requiring a JRE
Native Image
12
14. GraalVMvsScalaNative
• Applies to the whole JVM ecosystem
• Java, Scala, Kotlin, Clojure, …
• Bigger app binaries size
• Bigger community, bigger sponsor: Oracle
• Framework support, related projects
• Spring, Quarkus, sbt-native-packager
• native-image is early adopter technology
• Hard to configure it right
• Polyglot features
14
• Only applies to Scala
• Possibly more optimised?
• Smaller community, smaller sponsor:
Scala Center
• Smaller libraries support
• sbt-crossproject
• Not updated for a while, now restarted
• Only supports Scala 2.11
• Interoperability with C
16. References2/2
• Scala Native
https://scala-native.readthedocs.io/en/latest/index.html
https://github.com/scala-native/scala-native
• Scala Native next steps
https://contributors.scala-lang.org/t/scala-native-next-steps/4216
• A 7MB native-image Java app that runs in 30ms and uses only 4MB of RAM!
https://sites.google.com/a/athaydes.com/renato-athaydes/posts/a7mbnative-
imagejavaappthatrunsin30msandusesonly4mbofram
• Javalin web framework with GraalVM
https://javalin.io/2018/09/27/javalin-graalvm-example.html
• Example benchmarks for REST APIs with GraalVM
https://www.rubix.nl/blogs/graalvm-native-images-rest-api/
• Top 10 Things To Do With GraalVM
https://medium.com/graalvm/graalvm-ten-things-12d9111f307d
16