Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

Runtime.maxMemory() DOES change @ runtime: I wondered getting Total-ram > Max-ram and MemoryControl.available() < 0 MemoryControl.available() < 0 causes some errors where its value is used for dimension of buffers for eg.

sixcooler916d79111e96
diff --git a/htroot/PerformanceMemory_p.java b/htroot/PerformanceMemory_p.java
index 66b164e00..51dfbb81a 100644
--- a/htroot/PerformanceMemory_p.java
+++ b/htroot/PerformanceMemory_p.java
@@ -73,11 +73,11 @@ public class PerformanceMemory_p {
final long memoryTotalAfterInitAGC = env.getConfigLong("memoryTotalAfterInitAGC", 0L);
final long memoryTotalAfterStartup = env.getConfigLong("memoryTotalAfterStartup", 0L);
- prop.putNum("memoryMax", MemoryControl.maxMemory / MB);
- prop.putNum("memoryAvailAfterStartup", (MemoryControl.maxMemory - memoryTotalAfterStartup + memoryFreeAfterStartup) / MB);
- prop.putNum("memoryAvailAfterInitBGC", (MemoryControl.maxMemory - memoryTotalAfterInitBGC + memoryFreeAfterInitBGC) / MB);
- prop.putNum("memoryAvailAfterInitAGC", (MemoryControl.maxMemory - memoryTotalAfterInitAGC + memoryFreeAfterInitAGC) / MB);
- prop.putNum("memoryAvailNow", (MemoryControl.maxMemory - memoryTotalNow + memoryFreeNow) / MB);
+ prop.putNum("memoryMax", MemoryControl.maxMemory() / MB);
+ prop.putNum("memoryAvailAfterStartup", (MemoryControl.maxMemory() - memoryTotalAfterStartup + memoryFreeAfterStartup) / MB);
+ prop.putNum("memoryAvailAfterInitBGC", (MemoryControl.maxMemory() - memoryTotalAfterInitBGC + memoryFreeAfterInitBGC) / MB);
+ prop.putNum("memoryAvailAfterInitAGC", (MemoryControl.maxMemory() - memoryTotalAfterInitAGC + memoryFreeAfterInitAGC) / MB);
+ prop.putNum("memoryAvailNow", (MemoryControl.maxMemory() - memoryTotalNow + memoryFreeNow) / MB);
prop.putNum("memoryTotalAfterStartup", memoryTotalAfterStartup / KB);
prop.putNum("memoryTotalAfterInitBGC", memoryTotalAfterInitBGC / KB);
prop.putNum("memoryTotalAfterInitAGC", memoryTotalAfterInitAGC / KB);
diff --git a/htroot/Status.java b/htroot/Status.java
index a81eab0e4..9474c791b 100644
--- a/htroot/Status.java
+++ b/htroot/Status.java
@@ -287,7 +287,7 @@ public class Status {
// memory usage and system attributes
prop.put("freeMemory", Formatter.bytesToString(MemoryControl.free()));
prop.put("totalMemory", Formatter.bytesToString(MemoryControl.total()));
- prop.put("maxMemory", Formatter.bytesToString(MemoryControl.maxMemory));
+ prop.put("maxMemory", Formatter.bytesToString(MemoryControl.maxMemory()));
prop.put("processors", WorkflowProcessor.availableCPU);
// proxy traffic
diff --git a/htroot/api/status_p.java b/htroot/api/status_p.java
index 73f0c9c50..262a35f2f 100644
--- a/htroot/api/status_p.java
+++ b/htroot/api/status_p.java
@@ -46,7 +46,7 @@ public class status_p {
// memory usage and system attributes
prop.putNum("freeMemory", MemoryControl.free());
prop.putNum("totalMemory", MemoryControl.total());
- prop.putNum("maxMemory", MemoryControl.maxMemory);
+ prop.putNum("maxMemory", MemoryControl.maxMemory());
prop.putNum("processors", WorkflowProcessor.availableCPU);
// proxy traffic
diff --git a/source/net/yacy/dbtest.java b/source/net/yacy/dbtest.java
index 54cee9389..6d55117e9 100644
--- a/source/net/yacy/dbtest.java
+++ b/source/net/yacy/dbtest.java
@@ -384,12 +384,12 @@ public class dbtest {
System.out.println("Loop " + loop + ": Write = " + write + ", Remove = " + remove);
System.out.println(" bevore GC: " +
"free = " + MemoryControl.free() +
- ", max = " + MemoryControl.maxMemory +
+ ", max = " + MemoryControl.maxMemory() +
", total = " + MemoryControl.total());
System.gc();
System.out.println(" after GC: " +
"free = " + MemoryControl.free() +
- ", max = " + MemoryControl.maxMemory +
+ ", max = " + MemoryControl.maxMemory() +
", total = " + MemoryControl.total());
loop++;
}
diff --git a/source/net/yacy/kelondro/util/MemoryControl.java b/source/net/yacy/kelondro/util/MemoryControl.java
index 9f8d6284e..6f72ee17c 100644
--- a/source/net/yacy/kelondro/util/MemoryControl.java
+++ b/source/net/yacy/kelondro/util/MemoryControl.java
@@ -34,7 +34,6 @@ public class MemoryControl {
private static final Runtime runtime = Runtime.getRuntime();
- public static long maxMemory = runtime.maxMemory(); // this value does never change during runtime
private static final Log log = new Log("MEMORY");
private static final long[] gcs = new long[5];
@@ -103,7 +102,16 @@ public class MemoryControl {
* @return bytes
*/
public static final long available() {
- return maxMemory - total() + free();
+ return maxMemory() - total() + free();
+ }
+
+ /**
+ * maximum memory the Java virtual will allocate machine; may vary over time in some cases
+ * @return bytes
+ */
+ public static final long maxMemory()
+ {
+ return runtime.maxMemory();
}
/**
@@ -225,16 +233,19 @@ public class MemoryControl {
// try this with a jvm 1.4.2 and with a jvm 1.5 and compare results
final int mb = 1024 * 1024;
System.out.println("vm: " + System.getProperty("java.vm.version"));
- System.out.println("computed max = " + (maxMemory / mb) + " mb");
- final int alloc = 10000;
+ System.out.println("computed max = " + (maxMemory() / mb) + " mb");
final byte[][] x = new byte[100000][];
for (int i = 0; i < 100000; i++) {
- x[i] = new byte[alloc];
- if (i % 100 == 0) System.out.println("used = " + (i * alloc / mb) +
- ", total = " + (total() / mb) +
- ", free = " + (free() / mb) +
- ", max = " + (maxMemory / mb) +
- ", avail = " + (available() / mb));
+ if (request(mb, false))
+ {
+ x[i] = new byte[mb];
+ System.out.println("used = " + i + " / " + (used() /mb) +
+ ", total = " + (total() / mb) +
+ ", free = " + (free() / mb) +
+ ", max = " + (maxMemory() / mb) +
+ ", avail = " + (available() / mb) +
+ ", averageGC = " + getAverageGCFree());
+ }
}
}
diff --git a/source/net/yacy/yacy.java b/source/net/yacy/yacy.java
index b5c75d8da..1b3e3431f 100644
--- a/source/net/yacy/yacy.java
+++ b/source/net/yacy/yacy.java
@@ -637,7 +637,7 @@ public final class yacy {
// db used to hold all neede urls
final MetadataRepository minimizedUrlDB = new MetadataRepository(new File(new File(indexRoot2, networkName), "TEXT"), "text.urlmd", false, false);
- final int cacheMem = (int)(MemoryControl.maxMemory - MemoryControl.total());
+ final int cacheMem = (int)(MemoryControl.maxMemory() - MemoryControl.total());
if (cacheMem < 2048000) throw new OutOfMemoryError("Not enough memory available to start clean up.");
final Segment wordIndex = new Segment(