@@ -51,7 +51,7 @@ class MemoryUtil {
5151 private static final long MAX_HEAP_BYTES = 32_000_000_000L ;
5252
5353 /* Use 80% of total system memory in case available memory cannot be determined. */
54- private static final double FALLBACK_MAX_RAM_PERCENTAGE = 80.0 ;
54+ private static final double FALLBACK_TOTAL_MEMORY_RATIO = 0.8 ;
5555
5656 public static List <String > determineMemoryFlags () {
5757 return List .of (
@@ -88,7 +88,13 @@ private static double determineReasonableMaxRAMPercentage() {
8888 case WINDOWS -> getAvailableMemorySizeWindows ();
8989 };
9090 if (reasonableMaxMemorySize < 0 || reasonableMaxMemorySize > totalMemorySize ) {
91- return FALLBACK_MAX_RAM_PERCENTAGE ;
91+ /*
92+ * The amount of available memory was not at all or incorrectly detected. Fall back to a
93+ * value based on total memory. OperatingSystemMXBean.getTotalMemorySize() is
94+ * container-aware whereas, for example, /proc/meminfo is not and thus may report
95+ * available memory of the host which can exceed the memory limit of a container.
96+ */
97+ reasonableMaxMemorySize = (long ) (totalMemorySize * FALLBACK_TOTAL_MEMORY_RATIO );
9298 }
9399 if (reasonableMaxMemorySize < MIN_HEAP_BYTES ) {
94100 throw new NativeImageError (
@@ -102,7 +108,9 @@ private static double determineReasonableMaxRAMPercentage() {
102108
103109 /**
104110 * Returns the total amount of available memory in bytes on Linux based on
105- * <code>/proc/meminfo</code>, otherwise <code>-1</code>.
111+ * <code>/proc/meminfo</code>, otherwise <code>-1</code>. Note that this metric is not
112+ * container-aware (does not take cgroups into account) and may report available memory of the
113+ * host.
106114 *
107115 * @see <a href=
108116 * "https://github.com/torvalds/linux/blob/865fdb08197e657c59e74a35fa32362b12397f58/mm/page_alloc.c#L5137">page_alloc.c#L5137</a>
0 commit comments