java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > GraalVM Native Image Basics

GraalVM系列Native Image Basics静态分析

作者:Hoeller

这篇文章主要为大家介绍了GraalVM系列Native Image Basics静态分析详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

引言

native image是GraalVM中提供的一个命令,可以把字节码文件或Jar包编译成为一个二进制可执行文件,同时它自己也是用Java语言开发的(实现了Java的语言自举)。

Build Time vs Run Time

native image在编译时,可能会执行类中的某些代码,比如给类中的static属性赋值(正常来说应该时运行时才去赋值的,现在是编译时可能就被赋值了,这里说的编译不是javac)。

通常,在Java中,一个类在第一次被使用时才会进行初始化,但是我们使用native image时就有可能直接进行类的初始化,我们把这个机制叫做build-time initialized,而二进制可执行文件在运行时,即便是第一次使用这个类时也都不会触发类的初始化了。

默认情况下native image是不会执行编译期类初始化的,我们可以通过两种方式来在编译时触发类的初始化:

native image会把常用的JDK中的类在编译时进行初始化,比如java.lang.Stringjava.util.**,等等。

编译期类的初始化是一个专业特征,并不是所有类都适合。

请看下面的Demo加深理解:

public class HelloWorld {
    static class Greeter {
        static {
            System.out.println("Greeter is getting ready!");
        }
        public static void greet() {
          System.out.println("Hello, World!");
        }
    }
  public static void main(String[] args) {
    Greeter.greet();
  }
}

使用Java原本的方式编译并运行:

javac HelloWorld.java
java HelloWorld 
Greeter is getting ready!
Hello, World!

然后,我们把它编译为一个本地可执行文件,然后执行这个文件:

native-image HelloWorld
===============================================================
GraalVM Native Image: Generating 'helloworld' (executable)...
================================================================
...
Finished generating 'helloworld' in 14.9s.
./helloworld 
Greeter is getting ready!
Hello, World!

我们发现,上述两个过程都是在运行时才会对HelloWorld类进行初始化,所以默认情况下不会进行类的初始化。

我们通过添加--initialize-at-build-time=HelloWorld\$Greeter来看看编译期类初始化是怎样的:

native-image HelloWorld --initialize-at-build-time=HelloWorld\$Greeter
======================================================================
GraalVM Native Image: Generating 'helloworld' (executable)...
======================================================================
Greeter is getting ready!
...
Finished generating 'helloworld' in 13.6s.
./helloworld 
Hello, World!

我们发现Greeter is getting ready!是在编译时打印出来的,而真正在运行时由于HelloWorld类已经被初始化了,所以就没有再初始化了。而在编译时类初始化过程中被赋值的静态属性,会保存在二进制可执行文件中的image heap中。

Native Image Heap

Native Image heap也可以叫做image heap,它包含了:

可以通过编译时类初始化把一个对象放入image heap中:

class Example {
    private static final String message;
    static {
        message = System.getProperty("message");
    }
    public static void main(String[] args) {
        System.out.println("Hello, World! My message is: " + message);
    }
}

正常用java运行:

javac Example.java
java -Dmessage=hi Example
Hello, World! My message is: hi
java -Dmessage=hello Example 
Hello, World! My message is: hello
java Example
Hello, World! My message is: null

而如果使用编译期类初始化:

native-image Example --initialize-at-build-time=Example -Dmessage=native
========================================================================
GraalVM Native Image: Generating 'example' (executable)...
========================================================================
...
Finished generating 'example' in 19.0s.
./example 
Hello, World! My message is: native
./example -Dmessage=aNewMessage
Hello, World! My message is: native

Example类的初始化在编译期被执行了,并且会创建一个String对象赋值给message属性,并且把它存进了image heap中,运行的时候就直接从image heap中拿出来用了,忽略了运行时指定的-Dmessage

静态分析

native image在执行时,会先进行静态分析,静态分析会扫描出当前应用程序中真正用到了哪些类、方法、属性(其实通常我们一个应用中很多类,特别是依赖的第三方Jar包中的类,是没有被应用程序使用的),这些元素称之为reachable code

静态分析包含两个部分:

只有可达元素才能包含到二进制可执行文件中,一个二进制可执行文件编译出来后,运行过程中就不能再有新元素被添加进去了,比如动态类加载,我们把这个叫做closed-world。

官网原文  https://www.graalvm.org/latest/reference-manual/native-image/basics/

以上就是GraalVM系列Native Image Basics静态分析的详细内容,更多关于GraalVM Native Image Basics的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文