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.

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7852 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
sixcooler 14 years ago
parent 3a5fa73008
commit 916d79111e

@ -73,11 +73,11 @@ public class PerformanceMemory_p {
final long memoryTotalAfterInitAGC = env.getConfigLong("memoryTotalAfterInitAGC", 0L); final long memoryTotalAfterInitAGC = env.getConfigLong("memoryTotalAfterInitAGC", 0L);
final long memoryTotalAfterStartup = env.getConfigLong("memoryTotalAfterStartup", 0L); final long memoryTotalAfterStartup = env.getConfigLong("memoryTotalAfterStartup", 0L);
prop.putNum("memoryMax", MemoryControl.maxMemory / MB); prop.putNum("memoryMax", MemoryControl.maxMemory() / MB);
prop.putNum("memoryAvailAfterStartup", (MemoryControl.maxMemory - memoryTotalAfterStartup + memoryFreeAfterStartup) / MB); prop.putNum("memoryAvailAfterStartup", (MemoryControl.maxMemory() - memoryTotalAfterStartup + memoryFreeAfterStartup) / MB);
prop.putNum("memoryAvailAfterInitBGC", (MemoryControl.maxMemory - memoryTotalAfterInitBGC + memoryFreeAfterInitBGC) / MB); prop.putNum("memoryAvailAfterInitBGC", (MemoryControl.maxMemory() - memoryTotalAfterInitBGC + memoryFreeAfterInitBGC) / MB);
prop.putNum("memoryAvailAfterInitAGC", (MemoryControl.maxMemory - memoryTotalAfterInitAGC + memoryFreeAfterInitAGC) / MB); prop.putNum("memoryAvailAfterInitAGC", (MemoryControl.maxMemory() - memoryTotalAfterInitAGC + memoryFreeAfterInitAGC) / MB);
prop.putNum("memoryAvailNow", (MemoryControl.maxMemory - memoryTotalNow + memoryFreeNow) / MB); prop.putNum("memoryAvailNow", (MemoryControl.maxMemory() - memoryTotalNow + memoryFreeNow) / MB);
prop.putNum("memoryTotalAfterStartup", memoryTotalAfterStartup / KB); prop.putNum("memoryTotalAfterStartup", memoryTotalAfterStartup / KB);
prop.putNum("memoryTotalAfterInitBGC", memoryTotalAfterInitBGC / KB); prop.putNum("memoryTotalAfterInitBGC", memoryTotalAfterInitBGC / KB);
prop.putNum("memoryTotalAfterInitAGC", memoryTotalAfterInitAGC / KB); prop.putNum("memoryTotalAfterInitAGC", memoryTotalAfterInitAGC / KB);

@ -287,7 +287,7 @@ public class Status {
// memory usage and system attributes // memory usage and system attributes
prop.put("freeMemory", Formatter.bytesToString(MemoryControl.free())); prop.put("freeMemory", Formatter.bytesToString(MemoryControl.free()));
prop.put("totalMemory", Formatter.bytesToString(MemoryControl.total())); 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); prop.put("processors", WorkflowProcessor.availableCPU);
// proxy traffic // proxy traffic

@ -46,7 +46,7 @@ public class status_p {
// memory usage and system attributes // memory usage and system attributes
prop.putNum("freeMemory", MemoryControl.free()); prop.putNum("freeMemory", MemoryControl.free());
prop.putNum("totalMemory", MemoryControl.total()); prop.putNum("totalMemory", MemoryControl.total());
prop.putNum("maxMemory", MemoryControl.maxMemory); prop.putNum("maxMemory", MemoryControl.maxMemory());
prop.putNum("processors", WorkflowProcessor.availableCPU); prop.putNum("processors", WorkflowProcessor.availableCPU);
// proxy traffic // proxy traffic

@ -384,12 +384,12 @@ public class dbtest {
System.out.println("Loop " + loop + ": Write = " + write + ", Remove = " + remove); System.out.println("Loop " + loop + ": Write = " + write + ", Remove = " + remove);
System.out.println(" bevore GC: " + System.out.println(" bevore GC: " +
"free = " + MemoryControl.free() + "free = " + MemoryControl.free() +
", max = " + MemoryControl.maxMemory + ", max = " + MemoryControl.maxMemory() +
", total = " + MemoryControl.total()); ", total = " + MemoryControl.total());
System.gc(); System.gc();
System.out.println(" after GC: " + System.out.println(" after GC: " +
"free = " + MemoryControl.free() + "free = " + MemoryControl.free() +
", max = " + MemoryControl.maxMemory + ", max = " + MemoryControl.maxMemory() +
", total = " + MemoryControl.total()); ", total = " + MemoryControl.total());
loop++; loop++;
} }

@ -34,7 +34,6 @@ public class MemoryControl {
private static final Runtime runtime = Runtime.getRuntime(); 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 Log log = new Log("MEMORY");
private static final long[] gcs = new long[5]; private static final long[] gcs = new long[5];
@ -103,7 +102,16 @@ public class MemoryControl {
* @return bytes * @return bytes
*/ */
public static final long available() { 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 // try this with a jvm 1.4.2 and with a jvm 1.5 and compare results
final int mb = 1024 * 1024; final int mb = 1024 * 1024;
System.out.println("vm: " + System.getProperty("java.vm.version")); System.out.println("vm: " + System.getProperty("java.vm.version"));
System.out.println("computed max = " + (maxMemory / mb) + " mb"); System.out.println("computed max = " + (maxMemory() / mb) + " mb");
final int alloc = 10000;
final byte[][] x = new byte[100000][]; final byte[][] x = new byte[100000][];
for (int i = 0; i < 100000; i++) { for (int i = 0; i < 100000; i++) {
x[i] = new byte[alloc]; if (request(mb, false))
if (i % 100 == 0) System.out.println("used = " + (i * alloc / mb) + {
", total = " + (total() / mb) + x[i] = new byte[mb];
", free = " + (free() / mb) + System.out.println("used = " + i + " / " + (used() /mb) +
", max = " + (maxMemory / mb) + ", total = " + (total() / mb) +
", avail = " + (available() / mb)); ", free = " + (free() / mb) +
", max = " + (maxMemory() / mb) +
", avail = " + (available() / mb) +
", averageGC = " + getAverageGCFree());
}
} }
} }

@ -637,7 +637,7 @@ public final class yacy {
// db used to hold all neede urls // db used to hold all neede urls
final MetadataRepository minimizedUrlDB = new MetadataRepository(new File(new File(indexRoot2, networkName), "TEXT"), "text.urlmd", false, false); 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."); if (cacheMem < 2048000) throw new OutOfMemoryError("Not enough memory available to start clean up.");
final Segment wordIndex = new Segment( final Segment wordIndex = new Segment(

Loading…
Cancel
Save