Skip to content

Commit f262f81

Browse files
committed
Include direct memory and non-heap memory in ML memory calculations.
1 parent abf5f00 commit f262f81

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

distribution/tools/server-cli/src/main/java/org/elasticsearch/server/cli/MachineDependentHeap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected int getHeapSizeMb(Settings nodeSettings, MachineNodeRole role, long av
9595
*
9696
* If this formula is changed then corresponding changes must be made to the {@code NativeMemoryCalculator} and
9797
* {@code MlAutoscalingDeciderServiceTests} classes in the ML plugin code. Failure to keep the logic synchronized
98-
* could result in repeated autoscaling up and down.
98+
* could result in ML processes crashing with OOM errors or repeated autoscaling up and down.
9999
*/
100100
case ML_ONLY -> {
101101
if (availableMemory <= (GB * 16)) {

server/src/main/java/org/elasticsearch/monitor/jvm/JvmInfo.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,7 @@ public class JvmInfo implements ReportingService.Info {
4343
long nonHeapInit = memoryMXBean.getNonHeapMemoryUsage().getInit() < 0 ? 0 : memoryMXBean.getNonHeapMemoryUsage().getInit();
4444
long nonHeapMax = memoryMXBean.getNonHeapMemoryUsage().getMax() < 0 ? 0 : memoryMXBean.getNonHeapMemoryUsage().getMax();
4545
long directMemoryMax = 0;
46-
try {
47-
Class<?> vmClass = Class.forName("sun.misc.VM");
48-
directMemoryMax = (Long) vmClass.getMethod("maxDirectMemory").invoke(null);
49-
} catch (Exception t) {
50-
// ignore
51-
}
5246
String[] inputArguments = runtimeMXBean.getInputArguments().toArray(new String[runtimeMXBean.getInputArguments().size()]);
53-
Mem mem = new Mem(heapInit, heapMax, nonHeapInit, nonHeapMax, directMemoryMax);
5447

5548
String bootClassPath;
5649
try {
@@ -130,6 +123,11 @@ public class JvmInfo implements ReportingService.Info {
130123
configuredMaxHeapSize = Long.parseLong((String) valueMethod.invoke(maxHeapSizeVmOptionObject));
131124
} catch (Exception ignored) {}
132125

126+
try {
127+
Object maxDirectMemorySizeVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "MaxDirectMemorySize");
128+
directMemoryMax = Long.parseLong((String) valueMethod.invoke(maxDirectMemorySizeVmOptionObject));
129+
} catch (Exception ignored) {}
130+
133131
try {
134132
Object useSerialGCVmOptionObject = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "UseSerialGC");
135133
useSerialGC = (String) valueMethod.invoke(useSerialGCVmOptionObject);
@@ -139,6 +137,8 @@ public class JvmInfo implements ReportingService.Info {
139137

140138
}
141139

140+
Mem mem = new Mem(heapInit, heapMax, nonHeapInit, nonHeapMax, directMemoryMax);
141+
142142
INSTANCE = new JvmInfo(
143143
ProcessHandle.current().pid(),
144144
System.getProperty("java.version"),
@@ -496,5 +496,8 @@ public ByteSizeValue getHeapMax() {
496496
return ByteSizeValue.ofBytes(heapMax);
497497
}
498498

499+
public ByteSizeValue getTotalMax() {
500+
return ByteSizeValue.ofBytes(heapMax + nonHeapMax + directMemoryMax);
501+
}
499502
}
500503
}

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearning.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,8 @@ public Settings additionalSettings() {
845845
machineMemoryAttrName,
846846
Long.toString(OsProbe.getInstance().osStats().getMem().getAdjustedTotal().getBytes())
847847
);
848-
addMlNodeAttribute(additionalSettings, jvmSizeAttrName, Long.toString(Runtime.getRuntime().maxMemory()));
848+
849+
addMlNodeAttribute(additionalSettings, jvmSizeAttrName, Long.toString(JvmInfo.jvmInfo().getMem().getTotalMax().getBytes()));
849850
addMlNodeAttribute(
850851
additionalSettings,
851852
deprecatedAllocatedProcessorsAttrName,

0 commit comments

Comments
 (0)