Skip to content

Commit 55a4bc6

Browse files
committed
fix: auto tune of MAX_PAGE_RAM
Fixed issue #3580
1 parent 64eb57e commit 55a4bc6

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

engine/src/main/java/com/arcadedb/GlobalConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,20 @@ public Object call(final Object value) {
137137
final long maxRAM = ((long) value) * 1024 * 1024; // VALUE IN MB
138138

139139
if (maxRAM > Runtime.getRuntime().maxMemory() * 80 / 100) {
140-
final long newValue = Runtime.getRuntime().maxMemory() / 2;
140+
final long newValueBytes = Runtime.getRuntime().maxMemory() / 2;
141+
final long newValueMB = newValueBytes / 1024 / 1024;
141142
if (LogManager.instance() != null)
142143
LogManager.instance()
143144
.log(this, Level.WARNING, "Setting '%s=%s' is > than 80%% of maximum heap (%s). Decreasing it to %s",
144145
MAX_PAGE_RAM.key, FileUtils.getSizeAsString(maxRAM),
145-
FileUtils.getSizeAsString(Runtime.getRuntime().maxMemory()), FileUtils.getSizeAsString(newValue));
146+
FileUtils.getSizeAsString(Runtime.getRuntime().maxMemory()), FileUtils.getSizeAsString(newValueBytes));
146147
else
147148
System.out.println(
148149
"Setting '%s=%s' is > than 80%% of maximum heap (%s). Decreasing it to %s".formatted(MAX_PAGE_RAM.key,
149150
FileUtils.getSizeAsString(maxRAM), FileUtils.getSizeAsString(Runtime.getRuntime().maxMemory()),
150-
FileUtils.getSizeAsString(newValue)));
151+
FileUtils.getSizeAsString(newValueBytes)));
151152

152-
return newValue;
153+
return newValueMB;
153154
}
154155
return value;
155156
}

engine/src/test/java/com/arcadedb/GlobalConfigurationTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,46 @@
2424
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2525

2626
class GlobalConfigurationTest extends TestHelper {
27+
@Test
28+
void maxPageRAMAutoTune() {
29+
final long originalValue = GlobalConfiguration.MAX_PAGE_RAM.getValueAsLong();
30+
31+
try {
32+
// When reset (no explicit value), maxPageRAM should be auto-tuned to ~25% of max heap
33+
GlobalConfiguration.MAX_PAGE_RAM.reset();
34+
final long autoTunedMB = GlobalConfiguration.MAX_PAGE_RAM.getValueAsLong();
35+
final long maxHeapMB = Runtime.getRuntime().maxMemory() / 1024 / 1024;
36+
37+
// Auto-tuned value should be approximately 25% of max heap, in MB
38+
assertThat(autoTunedMB).isLessThanOrEqualTo(maxHeapMB);
39+
assertThat(autoTunedMB).isEqualTo(Runtime.getRuntime().maxMemory() / 4 / 1024 / 1024);
40+
} finally {
41+
GlobalConfiguration.MAX_PAGE_RAM.setValue(originalValue);
42+
}
43+
}
44+
45+
@Test
46+
void maxPageRAMCorrectionReturnsMB() {
47+
final long originalValue = GlobalConfiguration.MAX_PAGE_RAM.getValueAsLong();
48+
49+
try {
50+
// Set maxPageRAM to a value exceeding 80% of heap to trigger correction
51+
final long maxHeapMB = Runtime.getRuntime().maxMemory() / 1024 / 1024;
52+
final long excessiveValueMB = maxHeapMB; // 100% of heap, definitely > 80%
53+
54+
GlobalConfiguration.MAX_PAGE_RAM.setValue(excessiveValueMB);
55+
final long correctedValue = GlobalConfiguration.MAX_PAGE_RAM.getValueAsLong();
56+
57+
// The corrected value must be in MB (should be ~50% of heap in MB)
58+
// Before the fix, this would return bytes instead of MB
59+
final long expectedMB = Runtime.getRuntime().maxMemory() / 2 / 1024 / 1024;
60+
assertThat(correctedValue).isEqualTo(expectedMB);
61+
assertThat(correctedValue).isLessThanOrEqualTo(maxHeapMB);
62+
} finally {
63+
GlobalConfiguration.MAX_PAGE_RAM.setValue(originalValue);
64+
}
65+
}
66+
2767
@Test
2868
void serverMode() {
2969
final String original = GlobalConfiguration.SERVER_MODE.getValueAsString();

0 commit comments

Comments
 (0)