27
27
import java .io .BufferedReader ;
28
28
import java .io .InputStreamReader ;
29
29
import java .lang .management .ManagementFactory ;
30
+ import java .lang .reflect .Method ;
30
31
import java .nio .file .Files ;
31
32
import java .nio .file .Paths ;
32
33
import java .util .ArrayList ;
@@ -65,6 +66,23 @@ class MemoryUtil {
65
66
*/
66
67
private static final long MAX_HEAP_BYTES = 32_000_000_000L ;
67
68
69
+ private static final Method IS_CONTAINERIZED_METHOD ;
70
+ private static final Object IS_CONTAINERIZED_RECEIVER ;
71
+
72
+ static {
73
+ IS_CONTAINERIZED_METHOD = ReflectionUtil .lookupMethod (jdk .jfr .internal .JVM .class , "isContainerized" );
74
+ if (JavaVersionUtil .JAVA_SPEC == 21 ) { // non-static
75
+ var jvmField = ReflectionUtil .lookupField (jdk .jfr .internal .JVM .class , "jvm" );
76
+ try {
77
+ IS_CONTAINERIZED_RECEIVER = jvmField .get (null );
78
+ } catch (IllegalAccessException e ) {
79
+ throw VMError .shouldNotReachHere (e );
80
+ }
81
+ } else {
82
+ IS_CONTAINERIZED_RECEIVER = null ; // static
83
+ }
84
+ }
85
+
68
86
public static List <String > determineMemoryFlags (NativeImage .HostFlags hostFlags ) {
69
87
List <String > flags = new ArrayList <>();
70
88
if (hostFlags .hasUseParallelGC ()) {
@@ -77,9 +95,9 @@ public static List<String> determineMemoryFlags(NativeImage.HostFlags hostFlags)
77
95
* -XX:InitialRAMPercentage or -Xms.
78
96
*/
79
97
if (hostFlags .hasMaxRAMPercentage ()) {
80
- flags .addAll (determineReasonableMaxRAMPercentage (value -> "-XX:MaxRAMPercentage=" + value ));
98
+ flags .addAll (determineMemoryUsageFlags (value -> "-XX:MaxRAMPercentage=" + value ));
81
99
} else if (hostFlags .hasMaximumHeapSizePercent ()) {
82
- flags .addAll (determineReasonableMaxRAMPercentage (value -> "-XX:MaximumHeapSizePercent=" + value .intValue ()));
100
+ flags .addAll (determineMemoryUsageFlags (value -> "-XX:MaximumHeapSizePercent=" + value .intValue ()));
83
101
}
84
102
if (hostFlags .hasGCTimeRatio ()) {
85
103
/*
@@ -98,13 +116,12 @@ public static List<String> determineMemoryFlags(NativeImage.HostFlags hostFlags)
98
116
}
99
117
100
118
/**
101
- * Returns a percentage (0.0-100.0) to be used as a value for the -XX:MaxRAMPercentage or
102
- * -XX:MaximumHeapSizePercent flags of the builder process. Dedicated mode uses a fixed
103
- * percentage of total memory and is the default in containers. Shared mode tries to use
104
- * available memory to reduce memory pressure on the host machine. Note that this method uses
105
- * OperatingSystemMXBean, which is container-aware.
119
+ * Returns memory usage flags for the build process. Dedicated mode uses a fixed percentage of
120
+ * total memory and is the default in containers. Shared mode tries to use available memory to
121
+ * reduce memory pressure on the host machine. Note that this method uses OperatingSystemMXBean,
122
+ * which is container-aware.
106
123
*/
107
- private static List <String > determineReasonableMaxRAMPercentage (Function <Double , String > toMemoryFlag ) {
124
+ private static List <String > determineMemoryUsageFlags (Function <Double , String > toMemoryFlag ) {
108
125
var osBean = (com .sun .management .OperatingSystemMXBean ) ManagementFactory .getOperatingSystemMXBean ();
109
126
final double totalMemorySize = osBean .getTotalMemorySize ();
110
127
final double dedicatedMemorySize = totalMemorySize * DEDICATED_MODE_TOTAL_MEMORY_RATIO ;
@@ -151,18 +168,11 @@ private static List<String> determineReasonableMaxRAMPercentage(Function<Double,
151
168
152
169
private static boolean isContainerized () {
153
170
/*
154
- * [GR-55515]: Accessing isContainerized() reflectively for 21 JDK compatibility (non-static
155
- * vs static method). After dropping JDK 21, use it directly.
171
+ * [GR-55515]: Accessing isContainerized() reflectively only for 21 JDK compatibility
172
+ * (non-static vs static method). After dropping JDK 21, use it directly.
156
173
*/
157
- var isContainerized = ReflectionUtil .lookupMethod (jdk .jfr .internal .JVM .class , "isContainerized" );
158
174
try {
159
- final Object receiver ;
160
- if (JavaVersionUtil .JAVA_SPEC == 21 ) { // non-static
161
- receiver = ReflectionUtil .lookupField (jdk .jfr .internal .JVM .class , "jvm" ).get (null );
162
- } else {
163
- receiver = null ; // static
164
- }
165
- return (boolean ) isContainerized .invoke (receiver );
175
+ return (boolean ) IS_CONTAINERIZED_METHOD .invoke (IS_CONTAINERIZED_RECEIVER );
166
176
} catch (ReflectiveOperationException | ClassCastException e ) {
167
177
throw VMError .shouldNotReachHere (e );
168
178
}
0 commit comments