preface

GraalVM helps developers make better use of Java in three main ways:

  1. By making it faster using a state-of-the-art JIT compiler,
  2. Compile them into binary executables that start up on time and consume low memory,
  3. Programming with supported multiple languages or code.

This quick reference is a short one-page summary of what GraalVM can do and the key options and commands used to illustrate its capabilities.

GraalVM Quick Reference (Print in HD PDF)

You can download and print it out on A4 paper and hang it in your office to remind GraalVM of what can be done and what configuration options are available. Be sure to print the PDF version (hd without code). Please note that if your printer is more adapted to the American alphabet paper format, please use this version of the PDF.

In this article, we’ll take a quick look at the information in the reference and cover it in detail.

First, run Java applications

The first part focuses on running Java applications. First, remember that the GraalVM installation package includes the JDK, which means you can use GraalVM as the JDK. For example, compiling Java source code using Javac:

javac MyApp.java
Copy the code

Of course, you can also use GraalVM to run Java applications or any other JVM language. In addition to being able to run Java, GraalVM benefits from its powerful just-in-time (JIT) compiler and achieves state-of-the-art peak performance, typically running faster than other JDKS.

java -jar MyApp.jar
Copy the code

When running Java applications using GraalVM, the underlying JVM uses Java Hotspot VM, which means that most configuration options are the same, for example, you can specify the application classpath like this:

java -cp target/myapp.jar com.example.Main
Copy the code

GraalVM compilers typically run in precompiled to native shared library mode, but their code is written in Java and can be used as JAR files, resulting in slightly different performance: compiling with heap memory comes at the expense of longer warm-up (for example, the compiler’s Java code needs to be compiled). The following options configure the mode you want to run (enabled by default) :

-XX:+-UseJVMCINativeLibrary
Copy the code

In addition to running the compiler as a JAR or native library, you can specify optimized configurations, economy to speed up warm-up, and Enterprise to get the best peak performance (GraalVM Enterprise edition, of course).

-Dgraal.CompilerConfiguration=enterprise|community|economy
Copy the code

A JIT compiler may speed up the application significantly, but sometimes it’s not clear if the compiler is working properly, if the code really gets to the top level, and which methods get to the compiler, you can turn on compile logging:

-Dgraal.PrintCompilation=true
Copy the code

In addition to viewing logs, you can also enable more debugging output, for example, printing compiler graphics to analyze them to find more room for optimization:

-Dgraal.Dump=:2
Copy the code

Of course, other FEATURES of the JVM also apply to GraalVM. For example, you can add a Java Agent that inserts instructions into code, generates classes at run time, and other Java Agent features. Both Java-based agents and native Agents can run. A typical example is the auxiliary configuration Agent used to simplify the construction of native image.

-javaagent:path/to/jar.jar
-agentlib:path/to/native/agent
Copy the code

Second, compile to binary executable file

The second major advantage of using GraalVM is its Native Image capability: the application is compiled into binaries ahead of time. To use it, you need to install the Native Image component first. A common approach is to download the component JAR file for the GraalVM distribution and then run the following command:

gu install -L native-image.jar
Copy the code

You can then use the installed Native Image component to generate binaries for your application:

native-image [options] MyClass
Copy the code

Alternatively, you can use JAR file syntax similar to Java commands:

native-image -jar MyApp.jar
Copy the code

Run the generated binaries directly:

./myApp
Copy the code

If you don’t want to build an executable, but just a shared library, use the –shared option. You’ll need to use the @centryPoint annotation to mark exposed methods, but a more detailed discussion of this topic is beyond the scope of this article.

--shared
Copy the code

Another very useful method is to build statically linked binaries, such as linking an operating system library like liBC into an executable. You can even choose which LIBC implementation to use. Glibc is used by default, muslc is optional, for which you need to configure the build environment.

--static --libc=muslc
Copy the code

Third, multi-language programming

You can run truffle-based language infrastructures: JavaScript, Ruby, Python, R, etc. This will include an interpreter for the language, a Truffle framework, and a JIT compiler so that code can be compiled at run time to speed up execution.

For example, each of these items includes support for the corresponding language:

--language:js
--language:python
--language:llvm
--language:ruby
Copy the code

Another attractive feature is configuration file boot optimization for native Image executables. You can generate a checked binary, apply the associated workload to it, record jIT-like profiles of executing code, and use these profiles to build production binaries.

Native image - PGO-instrument MyApp./ MyApp Native image - pGO profile.iprof MyAppCopy the code

Also, if you want to get a clearer picture of what happens during the generation of this native image, for example, trying to understand the class chain of class initialization, you can use some useful options.

The following options enable tracking the initialization path of a class:

-H:+TraceClassInitialization=package.class.Name
Copy the code

Native Image build is a Java process, so you can make a breakpoint in your code and see more details through the code debugger.

--debug-attach=[port]
Copy the code

In addition, there are a number of other useful options for configuring native Image build and runtime behavior. We’ll explore these options in the future, and you can also use the help to get some information available:

--expert-options-all
Copy the code

The third major advantage GraalVM gives you is a multilingual runtime that can run multiple languages, including the Node.js platform powered by GraalVM’s JavaScript engine. Therefore, if you have a Node application, you can run it by calling the Node command.

node myApp.js
Copy the code

In addition, there are a number of language initiators available to support the execution of programs:

js myApp.js
graalpython myApp.py
ruby myApp.rb
R myApp.r
lli myApp
Copy the code

By default, initiators (including Node) will run in Native mode, where the interpreter compiles to native Image binaries. So, to implement interoperability with the JVM using Java classes, use the — JVM option, or — Polyglot for other languages:

--polyglot --jvm
Copy the code

The language engine has a number of capabilities to limit the amount of resources, for example, the amount of time (milliseconds) a language context can run:

--sandbox.MaxCPUTime=Nms
Copy the code

General development tools

Last but not least, the GraalVM language supports general-purpose development tools out of the box. This is one of the most exciting parts of the entire GraalVM ecosystem: implementing a language interpreter to fix the semantics of the language and getting a powerful virtual machine, a set of GC algorithms, a debugger, a profiler, a memory profiler, and other free tools.

Specify the following options to enable the Chrome DevTools based debugger, sample profiler, trace profiler, and memory profiler, respectively:

--inspect
--cpusampler
--cputracer
--memsampler
Copy the code

conclusion

GraalVM is a versatile project that provides many interesting features that you can use in your applications, supporting Java, JVM language, JavaScript, Ruby, Python, R, and more.

From using a better just-in-time compiler, to building executables for your applications, to running components in different languages, you can now use GraalVM.

In this short reference article, we try to outline the most common options for different GraalVM features.

Download GraalVM quick Reference PDF, print it out and pin it to the wall. Make it a constant reminder of how much fun the GraalVM project is, even if you’re currently using only a few features.

While doing this, download GraalVM to try out some things from quick reference, such as running Java applications faster, using Native Images to make microservices more cloud-native, or using libraries in other languages to enhance our application!

.

Mica Rumeng Technology, PIG Leng, JustAuth Yadong, Wu Tiangou

Original link: medium.com/graalvm/gra…

The translator said

GraalVM is definitely a big part of Java’s future (5 to 10 years) and look forward to built-in support for GraalVM in Spring Boot 3 and Spring Framework 6.