Original: Taste of Little Sister (wechat official ID: XjjDog), welcome to share, please reserve the source.

Today we are going to introduce Spring-Native, which allows your Spring Boot application to experience the features of the GraalVM compiler and compile your application directly to Native!

No need to install jre and your application will run directly on the target machine, just like exe! And it starts in less than a second.

To experience this feature, take a demo from Spring Boot.

https://start.spring.io/
Copy the code

Select the experimental feature SpringNative here. Once downloaded, you can use Maven to package your tests.

mvn spring-boot:build-image
gradle bootBuildImage
Copy the code

Take a look at the incredible startup speed…. 0.038 seconds… Almost instantaneous!

Magic!

All this is thanks to the GraalVM compiler. However, you need to upgrade the JDK to at least 11 to use it, which is a different driver for heap Java8 users.

Of course, Spring Native is only supported in SpringBoot releases later than 2.4.5.

What is GraalVM?

Graalvm is also an Oracle project and its code address is:

https://github.com/oracle/graal
Copy the code

The project address is:

www.graalvm.org/docs/
Copy the code

Graalvm is a virtual machine that wants to unify the world. Compared to HotSpotVM, it can run other languages like Ruby, Python, PHP, etc.

It is a new JVM, but because of adaptation, it can have different languages running under the same VM.

Take a look at the chart below to see just how ambitious Graalvm is.

And that’s not all. The main attraction is the ability to package application code directly into native binary executable code that doesn’t even require a JVM to run.

As you all know, native and RUNNING in a VM are completely different, otherwise there wouldn’t be such a great technology as JIT. Even Android and ios know that native apps run much more smoothly than MonoTouch or Hybrid, making this feature more practical for enterprise applications that are looking for performance.

Surprisingly, it provides a bridge between languages. For example, I am optimistic about a library in JS, do not need to develop a new one, in Java directly can be used. This is because graalVM has developed cross-language interoperability protocols that guarantee interoperability across languages.

Most platforms already support this feature.

What is a native? Consider the following code.

public class Example {
    public static void main(String[] args) {
        String str = "Native Image is awesome";
        String reversed = reverseString(str);
        System.out.println("The reversed string is: " + reversed);
    }

    public static String reverseString(String str) {
        if (str.isEmpty())
            return str;
        return reverseString(str.substring(1)) + str.charAt(0); }}Copy the code

Typically, we run this directly, or package it as a JAR package.

javac Example.java
java Example
Copy the code

But we can go one step further and make the class file native.

native-image Example
Copy the code

To execute, just type./Example.

What are the benefits?

Apps compiled using Native can be launched in seconds, run faster, and use less memory. It is more compatible with mainstream deployment methods such as micro services and K8S.

However, it differs from traditional JVMS in a number of ways, including:

  1. A performance analysis of the system is provided at compile time
  2. Useless parts and code will not compile and will simply be removed, thanks to the modularity of previous Java versions
  3. Reflection, resources, and dynamic proxies need to be transformed ahead of time without class-loading delays
  4. The classpath is fixed at compile time
  5. Classes will not be lazily loaded and will be dumped into memory at startup

While Native has many benefits, it takes a long time to compile because it requires a lot of static code analysis, which is a common problem with all native programs.

End

With this thin JAR approach, does Java’s evolution feel more like Golang? Docker images will also be smaller and purer because of this change, and Java applications without the JRE will look more and more like real programs.

Unfortunately, although this idea of compiling to Native is good, it can’t compete with Golang at this stage, mainly due to compiler differences.

I hope GraalVM can continue to make efforts, with Java continue to fly for several decades, raising my three generations of descendants!

Xjjdog is a public account that doesn’t allow programmers to get sidetracked. Focus on infrastructure and Linux. Ten years architecture, ten billion daily flow, and you discuss the world of high concurrency, give you a different taste. My personal wechat xjjdog0, welcome to add friends, further communication.