You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, Native Image initializes application classes at runtime, except for the classes that Native Image proves "safe" for initialization at build time.
11
-
However, you can influence the default behavior by specifying the classes to be build-time or run-time initialized explicitly.
11
+
However, you can influence the default behavior by explicitly specifying the classes to be initialized at build-time or runtime.
12
12
For that, there are two command-line options: `--initialize-at-build-time` and `--initialize-at-run-time`.
13
13
You can use these options to specify whole packages or individual classes.
14
14
For example, if you have the classes `p.C1`, `p.C2`, … ,`p.Cn`, you can specify that all the classes in the package `p` are to be initialized at build time by passing the following option to `native-image`:
@@ -20,100 +20,62 @@ If you want only class `C1` in package `p` to be initialized at runtime, use:
20
20
--initialize-at-run-time=p.C1
21
21
```
22
22
23
-
You can also programmatically specify Class initialization using the [`RuntimeClassInitialization` class](https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/hosted/RuntimeClassInitialization.java) from the [Native Image Feature interface](https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/hosted/Feature.java).
23
+
You can also programmatically specify class initialization using the [`RuntimeClassInitialization`] class(https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/hosted/RuntimeClassInitialization.java) from the [Native Image Feature interface](https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/hosted/Feature.java).
24
24
25
25
This guide demonstrates how to build a native executable by running the class initializer at runtime (default behavior), and then at build time, and compares the two approaches.
26
26
27
27
### Prerequisite
28
28
Make sure you have installed a GraalVM JDK.
29
29
The easiest way to get started is with [SDKMAN!](https://sdkman.io/jdks#graal).
30
-
For other installation options, visit the [Downloads section](https://www.graalvm.org/downloads/).
30
+
For other installation options, visit the [Downloads](https://www.graalvm.org/downloads/) section.
31
31
32
32
## Run a Demo
33
33
34
-
For the demo part, you will run a simple Java application that uses an XML parser to list some Java talks from 2023.
35
-
The parser create records from the XML data and store them in a `List<Talk>` collection.
36
-
This approach removes the dependency on the DOM API for storing and manipulating the parsed data.
34
+
For the demo, run a simple Java application that parses some Java talks from 2023.
35
+
The parser creates records and adds them to a `List<Talk>` collection.
37
36
38
-
1. Save the following Java source code in a file named _XMLParser.java_:
37
+
1. Save the following Java source code in a file named _TalkParser.java_:
You can leave out the `--initialize-at-run-time=XMLParser,Talk` option in this example because these classes are marked for initialization at run time by default.
78
+
You can omit the `--initialize-at-run-time=TalkParser,Talk` option in this example because these classes are marked for initialization at run time by default.
117
79
The `-o` option specifies the name of the output file.
118
80
119
81
4. Run and `time` the native application:
@@ -122,56 +84,54 @@ This approach removes the dependency on the DOM API for storing and manipulating
122
84
```
123
85
On a machine with 16 GB of memory and 8 cores, you should see a result similar to:
124
86
```
125
-
Talks loaded via XML:
87
+
Talks loaded using scanner:
126
88
- Asynchronous Programming in Java: Options to Choose from by Venkat Subramaniam
127
89
- Anatomy of a Spring Boot App with Clean Architecture by Steve Pember
128
90
- Java in the Cloud with GraalVM by Alina Yurenko
129
91
- Bootiful Spring Boot 3 by Josh Long
130
92
./runtime-parser 0.00s user 0.00s system 52% cpu 0.010 total
131
93
```
132
-
The application parses the XML data at runtime.
94
+
The application parses the text block at runtime.
133
95
134
-
Check the file size which should be around 19M:
96
+
Check the file size which should be around 13M:
135
97
```
136
98
du -sh runtime-parser
137
99
```
138
100
139
-
5. Next, build a native executable initializing `XMLParser` at build time, and providing a different name for the output file to differentiate it from the previous build. The `Talk` classhas to be initialized explicitly too, so the objects of this type will be persisted in the image heap.
101
+
5. Next, build a native executable initializing `TalkParser` at build time, and providing a different name for the output file to differentiate it from the previous build. The `Talk` record has to be initialized explicitly too, so the objects of this type will be persisted in the executable heap.
If your application adds additional types to the image heap, each type (or the corresponding package) needs to be marked for build-time initialization explicitly to fullfil the requirements of `--strict-image-heap`.
106
+
If your application adds additional types to the executable heap, each type (or the corresponding package) needs to be marked for build-time initialization explicitly to fulfill the requirements of `--strict-image-heap`.
145
107
An appropriate actionable error message will guide you through the process.
146
108
147
-
Note that in this example, the static analysis optimizes `org.w3c.dom.Document` away.
148
-
149
109
6. Run and `time` the second executable for comparison:
150
110
```bash
151
111
time ./buildtime-parser
152
112
```
153
113
This time you should see something similar to this:
154
114
```
155
-
Talks loaded via XML:
115
+
Talks loaded using scanner:
156
116
- Asynchronous Programming in Java: Options to Choose from by Venkat Subramaniam
157
117
- Anatomy of a Spring Boot App with Clean Architecture by Steve Pember
158
118
- Java in the Cloud with GraalVM by Alina Yurenko
159
119
- Bootiful Spring Boot 3 by Josh Long
160
120
./buildtime-parser 0.00s user 0.00s system 53% cpu 0.016 total
161
121
```
162
-
Check the file size which should decrease to around 5.9M!
122
+
Check the file size which should decrease to around 6.4M!
163
123
```
164
124
du -sh buildtime-parser
165
125
```
166
-
The file size change is because Native Image runs the static initializer at build time, parsing the XML data, and persisting only the `Talks` objects in the executable.
167
-
As a result, the majority of the XML parsing infrastructure does not become reachable when Native Image statically analyzes the application and is, therefore, not included in the executable.
126
+
The file size change is because Native Image runs the static initializer at build time, parsing the text block, and persisting only the `Talk` records in the executable.
127
+
As a result, the majority of the scanning infrastructure does not become reachable when Native Image statically analyzes the application and is, therefore, not included in the executable.
168
128
169
129
Another valuable criterion for profiling applications more accurately is the number of instructions, which can be obtained using the [Linux `perf` profiler](../PerfProfiling.md).
170
130
171
131
For example, for this demo application, the number of instructions decreased by almost 30% (from 11.8M to 8.6M) in the case of build-time classinitialization:
172
132
```bash
173
133
perf stat ./runtime-parser
174
-
Talks loaded via XML:
134
+
Talks loaded using scanner:
175
135
- Asynchronous Programming in Java: Options to Choose from by Venkat Subramaniam
176
136
(...)
177
137
Performance counter stats for './runtime-parser':
@@ -187,7 +147,7 @@ Talks loaded via XML:
187
147
```
188
148
```bash
189
149
perf stat ./buildtime-parser
190
-
Talks loaded via XML:
150
+
Talks loaded using scanner:
191
151
- Asynchronous Programming in Java: Options to Choose from by Venkat Subramaniam
192
152
(...)
193
153
Performance counter stats for './buildtime-parser':
@@ -202,15 +162,15 @@ Talks loaded via XML:
202
162
0.002226000 seconds sys
203
163
```
204
164
205
-
This demonstrates how Native Image can shift work from runtime to build time: when the classis initialized at build time, the XML data is parsed when the executable is being built and only the parsed objects are included.
206
-
This not only makes the executable smaller in file size, but also faster to run: when the executable runs, the `Talk` objects already exists and only need to be printed.
165
+
This demonstrates how Native Image can shift work from runtime to build time: when the classis initialized at build time, the text block is parsed when the executable is being built and only the parsed objects are included.
166
+
This not only makes the executable smaller in file size, but also faster to run: when the executable runs, the `Talk` records already exist and only need to be printed.
207
167
208
168
To ensure native executables built with Native Image are as compatible as possible with the HotSpot behavior, application classes that cannot be safely initialized at build time, are initialized at runtime.
209
169
You as a user, or a framework that you use, must explicitly request build-time initialization for certain classes to benefit from smaller file sizes and faster times to run.
210
170
Include the right data structures to avoid the image size blowing up instead.
211
-
We also recommend to use `--initialize-at-build-time` with single classes only.
171
+
We also recommend using `--initialize-at-build-time` with single classes only.
212
172
It may be that you need to add a lot of `--initialize-at-build-time` entries.
213
-
Note that incorrect build-time initialization can lead from dysfunctional behavior to including sensitive data such as passwords or encryption keys, problems that are to be avoided in production settings.
173
+
Note that incorrect build-time initialization can lead to problems that are to be avoided in production settings such as dysfunctional behavior or including sensitive data such as passwords or encryption keys.
0 commit comments