From e16c25ddf7ac744fea355bc06a8de43c38a1b567 Mon Sep 17 00:00:00 2001
From: orbiter <orbiter@6c8d7289-2bf4-0310-a012-ef5d649a1542>
Date: Thu, 16 Apr 2009 22:45:39 +0000
Subject: [PATCH] (peak-) performance hacks

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@5819 6c8d7289-2bf4-0310-a012-ef5d649a1542
---
 htroot/PerformanceMemory_p.java               | 11 ++++-----
 htroot/Status.java                            |  2 +-
 htroot/api/status_p.java                      |  2 +-
 source/dbtest.java                            |  4 ++--
 .../anomic/kelondro/util/MemoryControl.java   | 23 ++++++++-----------
 source/yacy.java                              |  2 +-
 6 files changed, 19 insertions(+), 25 deletions(-)

diff --git a/htroot/PerformanceMemory_p.java b/htroot/PerformanceMemory_p.java
index 1939f0815..9bf425949 100644
--- a/htroot/PerformanceMemory_p.java
+++ b/htroot/PerformanceMemory_p.java
@@ -71,13 +71,12 @@ public class PerformanceMemory_p {
         final long memoryTotalAfterInitBGC = Long.parseLong(env.getConfig("memoryTotalAfterInitBGC", "0"));
         final long memoryTotalAfterInitAGC = Long.parseLong(env.getConfig("memoryTotalAfterInitAGC", "0"));
         final long memoryTotalAfterStartup = Long.parseLong(env.getConfig("memoryTotalAfterStartup", "0"));
-        final long memoryMax = MemoryControl.max();
         
-        prop.putNum("memoryMax", memoryMax / MB);
-        prop.putNum("memoryAvailAfterStartup", (memoryMax - memoryTotalAfterStartup + memoryFreeAfterStartup) / MB);
-        prop.putNum("memoryAvailAfterInitBGC", (memoryMax - memoryTotalAfterInitBGC + memoryFreeAfterInitBGC) / MB);
-        prop.putNum("memoryAvailAfterInitAGC", (memoryMax - memoryTotalAfterInitAGC + memoryFreeAfterInitAGC) / MB);
-        prop.putNum("memoryAvailNow", (memoryMax - 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 7adcf0cff..a0d98ca7d 100644
--- a/htroot/Status.java
+++ b/htroot/Status.java
@@ -273,7 +273,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.max()));
+        prop.put("maxMemory", Formatter.bytesToString(MemoryControl.maxMemory));
         prop.put("processors", serverProcessor.availableCPU);
 
         // proxy traffic
diff --git a/htroot/api/status_p.java b/htroot/api/status_p.java
index 27e719570..c1c6f7be7 100644
--- a/htroot/api/status_p.java
+++ b/htroot/api/status_p.java
@@ -30,7 +30,7 @@ public class status_p {
 		// memory usage and system attributes
         prop.putNum("freeMemory", MemoryControl.free());
         prop.putNum("totalMemory", MemoryControl.total());
-        prop.putNum("maxMemory", MemoryControl.max());
+        prop.putNum("maxMemory", MemoryControl.maxMemory);
         prop.putNum("processors", serverProcessor.availableCPU);
 
 		// proxy traffic
diff --git a/source/dbtest.java b/source/dbtest.java
index 966386bdf..2fadf58a4 100644
--- a/source/dbtest.java
+++ b/source/dbtest.java
@@ -377,12 +377,12 @@ public class dbtest {
                     System.out.println("Loop " + loop + ": Write = " + write + ", Remove = " + remove);
                     System.out.println(" bevore GC: " +
                               "free = " + MemoryControl.free() +
-                            ", max = " + MemoryControl.max() +
+                            ", max = " + MemoryControl.maxMemory +
                             ", total = " + MemoryControl.total());
                     System.gc();
                     System.out.println(" after  GC: " +
                             "free = " + MemoryControl.free() +
-                          ", max = " + MemoryControl.max() +
+                          ", max = " + MemoryControl.maxMemory +
                           ", total = " + MemoryControl.total());
                   loop++;
                 }
diff --git a/source/de/anomic/kelondro/util/MemoryControl.java b/source/de/anomic/kelondro/util/MemoryControl.java
index bdd3e9a5f..ca4277b84 100644
--- a/source/de/anomic/kelondro/util/MemoryControl.java
+++ b/source/de/anomic/kelondro/util/MemoryControl.java
@@ -32,8 +32,11 @@ import de.anomic.tools.Formatter;
  */
 public class MemoryControl {
 
+	
     private static final Runtime runtime = Runtime.getRuntime();
-    private static final Log log = new Log("MEMORY");
+    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];
     private static int gcs_pos = 0;
@@ -99,17 +102,8 @@ public class MemoryControl {
      * @return bytes
      */
     public static final long available() {
-        return max() - total() + free();
+        return maxMemory - total() + free();
     }
-    
-    /**
-     * maximum memory which the vm can use
-	 * @return bytes
-	 */
-	public static final long max()
-	{
-		return runtime.maxMemory();
-	}
 
 	/**
 	 * currently allocated memory in the Java virtual machine; may vary over time
@@ -140,13 +134,14 @@ public class MemoryControl {
      * @return whether enough memory could be freed (or is free) or not
      */
     public static boolean request(final long size, final boolean force) {
+    	final long avg = getAverageGCFree();
+    	if (avg >= size) return true;
         long avail = available();
         if (avail >= size) return true;
         if (log.isFine()) {
             final String t = new Throwable("Stack trace").getStackTrace()[1].toString();
             log.logFine(t + " requested " + (size >> 10) + " KB, got " + (avail >> 10) + " KB");
         } 
-        final long avg = getAverageGCFree();
         if (force || avg == 0 || avg + avail >= size) {
             // this is only called if we expect that an allocation of <size> bytes would cause the jvm to call the GC anyway
             
@@ -183,7 +178,7 @@ 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 = " + (max() / mb) + " mb");
+        System.out.println("computed max = " + (maxMemory / mb) + " mb");
         final int alloc = 10000;
         final byte[][] x = new byte[100000][];
         for (int i = 0; i < 100000; i++) {
@@ -191,7 +186,7 @@ public class MemoryControl {
             if (i % 100 == 0) System.out.println("used = " + (i * alloc / mb) +
                     ", total = " + (total() / mb) +
                     ", free = " + (free() / mb) +
-                    ", max = " + (max() / mb) +
+                    ", max = " + (maxMemory / mb) +
                     ", avail = " + (available() / mb));
         }
 
diff --git a/source/yacy.java b/source/yacy.java
index ba070e799..e53a704de 100644
--- a/source/yacy.java
+++ b/source/yacy.java
@@ -672,7 +672,7 @@ public final class yacy {
             // db used to hold all neede urls
             final MetadataRepository minimizedUrlDB = new MetadataRepository(new File(new File(indexRoot2, networkName), "TEXT"));
             
-            final int cacheMem = (int)(MemoryControl.max() - 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 plasmaWordIndex wordIndex = new plasmaWordIndex(networkName, log, indexPrimaryRoot, indexSecondaryRoot, 10000, false, 1, 0, false);