Skip to content

Running Java on CRaC

Published on
  • Java
  • Crac

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:

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!

Requirements

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

  1. Download OpenJDK build from https://github.com/CRaC/openjdk-builds/releases:
bash
$ wget https://github.com/CRaC/openjdk-builds/releases/download/17-crac%2B3/openjdk-17-crac+3_linux-x64.tar.gz
  1. Unpack the archive with sudo (without sudo it's not going to work):
bash
$ sudo tar zxf openjdk-17-crac+3_linux-x64.tar.gz
  1. Set JAVA_HOME to point to the OpenJDK CRaC directory:

For example, assuming that the JDK is located in /opt/:

bash
$ export JAVA_HOME=/opt/openjdk-17-crac+3_linux-x64/
  1. Include bin directory in PATH. Important: make sure that it's added before current path:
bash
export PATH=/opt/openjdk-17-crac+3_linux-x64/bin:$PATH

How to run

  1. Make sure that you have Maven 3.8.x installed, and it points to CRaC Java version:
bash
$ 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.

  1. Download a Jetty example:
bash
$ git clone https://github.com/CRaC/example-jetty
  1. Build project:
bash
$ mvn package
  1. Run JAR with flag -XX:CRaCCheckpointTo=cr:
bash
$ java -XX:CRaCCheckpointTo=cr -jar target/example-jetty-1.0-SNAPSHOT.jar
  1. Open new terminal window, go to the example-jetty and execute:
bash
$ jcmd target/example-jetty-1.0-SNAPSHOT.jar JDK.checkpoint

You should see that cr directory has been created in example-jetty directory.

  1. Now switch back to terminal when Jetty example is running, stop it, and start a project again, this time restoring from the checkpoint:
bash
$ java -XX:CRaCRestoreFrom=cr

Application should start in few milliseconds!

Conclusion

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 🍻.

What's next

I recommend looking into following guides to continue experimenting with CRaC:

Let's stay in touch and follow me on Twitter: @maciejwalkowiak

Subscribe to RSS feed