Running Java on CRaC
Russia has invaded Ukraine and already killed tens of thousands of civilians, with many more raped and tortured. Ukraine needs your help!
Have you heard about CRaC? It's an experimental project lead by Azul that researches a mechanism to checkpoint (make an image of, snapshot) a Java instance while it is executing and then restore from this snapshot.
Potentially it could solve typical JVM problems with slow startup & warm up times. Sounds familiar? Yes, there are quite a few initiatives in the Java world that address the very same problem.
I recommend learning more about CraC here:
- Superfast Application Startup: Java on CRaC
- Java on CRaC: Superfast JVM Application Startup by Simon Ritter
CRaC is not yet build into any official JDK and you won't find it in SDKMan.
It took me some time to figure out how to even run examples, so here's a step by step guide on how to set up a development machine to play with CRaC distribution of OpenJDK.
Big thanks to Sergio del Amo for hints!
There is no CRaC distribution for MacOS or Windows. I failed to get it running both as Docker container or on Gitpod (probably because it also runs on Docker).
To run CRaC you have to use Linux, or create a virtual machine for example with VirtualBox.
Also note - CRaC does not work on Ubuntu 22. I've managed to get it running on Ubuntu 18.04.
How to install
- Download OpenJDK build from https://github.com/CRaC/openjdk-builds/releases:
$ wget https://github.com/CRaC/openjdk-builds/releases/download/17-crac%2B3/openjdk-17-crac+3_linux-x64.tar.gz
- Unpack the archive with sudo (without sudo it's not going to work):
$ sudo tar zxf openjdk-17-crac+3_linux-x64.tar.gz
JAVA_HOMEto point to the OpenJDK CRaC directory:
For example, assuming that the JDK is located in
$ export JAVA_HOME=/opt/openjdk-17-crac+3_linux-x64/
PATH. Important: make sure that it's added before current path:
How to run
- Make sure that you have Maven 3.8.x installed, and it points to CRaC Java version:
$ mvn --version Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63) Maven home: /home/maciej/Downloads/maven/apache-maven-3.8.6 Java version: 17-crac, vendor: N/A, runtime: /home/maciej/Downloads/openjdk-17-crac+3_linux-x64 Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "5.4.0-84-generic", arch: "amd64", family: "unix"
If you are on Ubuntu 18.04 and run
apt-get install maven, you will get Maven 3.6 installed that is not compatible with Java 17. In such case, download Maven manually from https://maven.apache.org/download.cgi.
- Download a Jetty example:
$ git clone https://github.com/CRaC/example-jetty
- Build project:
$ mvn package
- Run JAR with flag
$ java -XX:CRaCCheckpointTo=cr -jar target/example-jetty-1.0-SNAPSHOT.jar
- Open new terminal window, go to the
$ jcmd target/example-jetty-1.0-SNAPSHOT.jar JDK.checkpoint
You should see that
cr directory has been created in
- Now switch back to terminal when Jetty example is running, stop it, and start a project again, this time restoring from the checkpoint:
$ java -XX:CRaCRestoreFrom=cr
Application should start in few milliseconds!
Does it mean that you can just take any application, create a snapshot and have it start in milliseconds? Not really. To make an application CRaC friendly, there must be no open sockets so application frameworks and underlying servlet containers like Tomcat, would have to ba adjusted to get it working.
There is a sample available for Spring Boot, but it's based on Boot 2.0.5, which isn't compatible with Java 17, which means to run it you would have to use old OpenJDK CRaC build - and you would have to build it yourself.
Micronaut is the only mainstream framework with the official support for CRaC: Micronaut CRaC. Kudos to Tim Yates and Sergio del Amo 🍻.
I recommend looking into following guides to continue experimenting with CRaC: