|
1 | | -# Getting Started |
| 1 | +# 그랄VM 예제 |
| 2 | +- 언어: Java 22 |
| 3 | +- 프레임워크: SpringBoot 3.2 |
| 4 | +- Apple Silicon M2 Pro |
| 5 | +- 운영체제: MacOS 14.0(Sonoma 23A344) |
2 | 6 |
|
3 | | - |
4 | 7 |
|
| 8 | +# GraalVM은 어떻게 동작하는가? |
| 9 | + |
| 10 | +nothing. |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +<!-- ###################################################################################################################################### --> |
| 16 | +<!-- ###################################################################################################################################### --> |
| 17 | +<!-- ###################################################################################################################################### --> |
| 18 | +<br/> |
| 19 | + |
| 20 | +# Example (순수 자바) |
| 21 | + |
| 22 | +### GraalVM 설치하기 |
| 23 | +설치하기 |
| 24 | +https://www.graalvm.org/downloads/ |
| 25 | + |
| 26 | +### 예제 코드 작성 |
| 27 | + |
| 28 | + |
| 29 | +```java |
| 30 | +// package graalvm.graalvmexample; // <- 이것을 작성하지 않도록 주의! |
| 31 | + |
| 32 | +public class HelloWorld { |
| 33 | + public static void main(String[] args) { |
| 34 | + System.out.println("HEllo World"); |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### 바이트코드로 컴파일하기 |
| 40 | +`javac` 명령을 통해 바이트코드로 컴파일한다. |
| 41 | +```shell |
| 42 | + ~/Doc/GitHub/SpringExample/graalvm-example/src/main/java/graalvm/graalvmexample GraalVM +11 !4 ?43 javac HelloWorld.java ✔ 14:28:04 |
| 43 | +``` |
| 44 | + |
| 45 | +### NativeImage로 만들기 |
| 46 | +`native-image` 명령을 통해 플랫폼에 맞는 기계어로 변환한다. |
| 47 | +```shell |
| 48 | + ~/Doc/GitHub/SpringExample/graalvm-example/src/main/java/graalvm/graalvmexample GraalVM +11 !4 ?44 native-image HelloWorld ✔ 14:32:50 |
| 49 | +======================================================================================================================== |
| 50 | +GraalVM Native Image: Generating 'helloworld' (executable)... |
| 51 | +======================================================================================================================== |
| 52 | +[1/8] Initializing... (11.4s @ 0.10GB) |
| 53 | + Java version: 22.0.2+9, vendor version: Oracle GraalVM 22.0.2+9.1 |
| 54 | + Graal compiler: optimization level: 2, target machine: armv8-a, PGO: off |
| 55 | + C compiler: cc (apple, arm64, 15.0.0) |
| 56 | + Garbage collector: Serial GC (max heap size: 80% of RAM) |
| 57 | + 1 user-specific feature(s): |
| 58 | + - com.oracle.svm.thirdparty.gson.GsonFeature |
| 59 | +------------------------------------------------------------------------------------------------------------------------ |
| 60 | +Build resources: |
| 61 | + - 12.09GB of memory (75.6% of 16.00GB system memory, determined at start) |
| 62 | + - 12 thread(s) (100.0% of 12 available processor(s), determined at start) |
| 63 | +[2/8] Performing analysis... [****] (3.7s @ 0.19GB) |
| 64 | + 2,056 reachable types (59.9% of 3,430 total) |
| 65 | + 1,849 reachable fields (38.8% of 4,760 total) |
| 66 | + 8,637 reachable methods (35.5% of 24,345 total) |
| 67 | + 778 types, 25 fields, and 348 methods registered for reflection |
| 68 | + 49 types, 33 fields, and 48 methods registered for JNI access |
| 69 | + 4 native libraries: -framework Foundation, dl, pthread, z |
| 70 | +[3/8] Building universe... (0.6s @ 0.21GB) |
| 71 | +[4/8] Parsing methods... [*] (0.4s @ 0.22GB) |
| 72 | +[5/8] Inlining methods... [***] (0.4s @ 0.26GB) |
| 73 | +[6/8] Compiling methods... [***] (6.9s @ 0.33GB) |
| 74 | +[7/8] Laying out methods... [*] (0.5s @ 0.34GB) |
| 75 | +[8/8] Creating image... [*] (0.9s @ 0.37GB) |
| 76 | + 2.62MB (44.08%) for code area: 3,897 compilation units |
| 77 | + 3.13MB (52.49%) for image heap: 51,987 objects and 71 resources |
| 78 | + 209.35kB ( 3.43%) for other data |
| 79 | + 5.95MB in total |
| 80 | +------------------------------------------------------------------------------------------------------------------------ |
| 81 | +Top 10 origins of code area: Top 10 object types in image heap: |
| 82 | + 1.26MB java.base 695.51kB byte[] for java.lang.String |
| 83 | + 1.14MB svm.jar (Native Image) 692.82kB byte[] for code metadata |
| 84 | + 80.79kB com.oracle.svm.svm_enterprise 364.24kB java.lang.String |
| 85 | + 25.46kB org.graalvm.nativeimage.base 332.24kB java.lang.Class |
| 86 | + 22.27kB jdk.proxy3 143.75kB java.util.HashMap$Node |
| 87 | + 21.27kB org.graalvm.collections 122.16kB heap alignment |
| 88 | + 19.61kB jdk.proxy1 114.52kB char[] |
| 89 | + 14.85kB jdk.graal.compiler 86.09kB java.lang.Object[] |
| 90 | + 14.50kB jdk.internal.vm.ci 80.48kB byte[] for reflection metadata |
| 91 | + 8.26kB jdk.proxy2 80.31kB com.oracle.svm.core.hub.DynamicHubCompanion |
| 92 | + 456.00B for 1 more packages 487.87kB for 532 more object types |
| 93 | + Use '-H:+BuildReport' to create a report with more details. |
| 94 | +------------------------------------------------------------------------------------------------------------------------ |
| 95 | +Security report: |
| 96 | + - Binary includes Java deserialization. |
| 97 | + - Use '--enable-sbom' to embed a Software Bill of Materials (SBOM) in the binary. |
| 98 | +------------------------------------------------------------------------------------------------------------------------ |
| 99 | +Recommendations: |
| 100 | + PGO: Use Profile-Guided Optimizations ('--pgo') for improved throughput. |
| 101 | + HEAP: Set max heap for improved and more predictable memory usage. |
| 102 | + CPU: Enable more CPU features with '-march=native' for improved performance. |
| 103 | + QBM: Use the quick build mode ('-Ob') to speed up builds during development. |
| 104 | +------------------------------------------------------------------------------------------------------------------------ |
| 105 | + 1.0s (3.9% of total time) in 408 GCs | Peak RSS: 0.69GB | CPU load: 4.23 |
| 106 | +------------------------------------------------------------------------------------------------------------------------ |
| 107 | +Build artifacts: |
| 108 | + /Users/hyeon/Documents/GitHub/SpringExample/graalvm-example/src/main/java/graalvm/graalvmexample/helloworld (executable) |
| 109 | +======================================================================================================================== |
| 110 | +Finished generating 'helloworld' in 25.3s. |
| 111 | +``` |
| 112 | + |
| 113 | +### 실행하기 |
| 114 | +생성된 helloworld를 실행할 수 있다. |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +<!-- ###################################################################################################################################### --> |
| 121 | +<!-- ###################################################################################################################################### --> |
| 122 | +<!-- ###################################################################################################################################### --> |
| 123 | +<br/> |
| 124 | + |
| 125 | +# Example (with spring boot) |
| 126 | + |
| 127 | +nothing. |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +<!-- ###################################################################################################################################### --> |
| 134 | +<!-- ###################################################################################################################################### --> |
| 135 | +<!-- ###################################################################################################################################### --> |
| 136 | +<br/> |
| 137 | + |
| 138 | +### 참고자료 |
| 139 | + |
| 140 | +시작하기 |
5 | 141 | https://www.graalvm.org/latest/docs/getting-started/ |
| 142 | + |
| 143 | +native-image 명령어도 설치해야함 |
| 144 | +https://www.graalvm.org/22.0/reference-manual/native-image/ |
| 145 | + |
| 146 | +### NoClassDefFoundError |
| 147 | +주의점! 예제를 만들때 `package`경로가 들어가지 않도록 주의한다. |
| 148 | +Link: https://stackoverflow.com/questions/17973970/how-can-i-solve-java-lang-noclassdeffounderror |
| 149 | +```shell |
| 150 | + ~/Doc/GitHub/SpringExample/graalvm-example/src/main/java/graalvm/graalvmexample GraalVM +11 !4 ?43 native-image HelloWorld ✔ 14:28:10 |
| 151 | +======================================================================================================================== |
| 152 | +GraalVM Native Image: Generating 'helloworld' (executable)... |
| 153 | +======================================================================================================================== |
| 154 | +[1/8] Initializing... (0.0s @ 0.07GB) |
| 155 | + |
| 156 | +The build process encountered an unexpected error: |
| 157 | + |
| 158 | +> java.lang.NoClassDefFoundError: HelloWorld (wrong name: graalvm/graalvmexample/HelloWorld) |
| 159 | + |
| 160 | +Please inspect the generated error report at: |
| 161 | +/Users/hyeon/Documents/GitHub/SpringExample/graalvm-example/src/main/java/graalvm/graalvmexample/svm_err_b_20240909T142820.972_pid24795.md |
| 162 | + |
| 163 | +If you are unable to resolve this problem, please file an issue with the error report at: |
| 164 | +https://graalvm.org/support |
| 165 | +``` |
| 166 | + |
| 167 | +- 에러 내용상세: svm_err_b_20240909T142820.972_pid24795.md |
| 168 | + |
| 169 | +### etc |
| 170 | +https://docs.spring.io/spring-boot/reference/packaging/native-image/index.html |
| 171 | + |
| 172 | +https://www.baeldung.com/graal-java-jit-compiler |
| 173 | + |
0 commit comments