Index: core/src/main/java/com/orientechnologies/orient/core/index/OIndexMVRBTreeAbstract.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- core/src/main/java/com/orientechnologies/orient/core/index/OIndexMVRBTreeAbstract.java	(revision 5536)
+++ core/src/main/java/com/orientechnologies/orient/core/index/OIndexMVRBTreeAbstract.java	(revision )
@@ -82,8 +82,10 @@
 
     type = iType;
     watchDog = new Listener() {
+      private final long memoryOptimizationThreshold = OGlobalConfiguration.MVRBTREE_MEMORY_OPTIMIZATION_THRESHOLD.getValueAsLong();
+
       public void memoryUsageLow(final long iFreeMemory, final long iFreeMemoryPercentage) {
-        map.setOptimization(iFreeMemoryPercentage < 10 ? 2 : 1);
+        map.setOptimization(iFreeMemoryPercentage < memoryOptimizationThreshold ? 2 : 1);
       }
     };
   }
Index: core/src/main/java/com/orientechnologies/orient/core/config/OGlobalConfiguration.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- core/src/main/java/com/orientechnologies/orient/core/config/OGlobalConfiguration.java	(revision 5536)
+++ core/src/main/java/com/orientechnologies/orient/core/config/OGlobalConfiguration.java	(revision )
@@ -135,6 +135,10 @@
       "Multiplicand factor to apply to entry-points list (parameter mvrbtree.entrypoints) to determine optimization is needed",
       Float.class, 1.0f),
 
+  MVRBTREE_MEMORY_OPTIMIZATION_THRESHOLD("mvrbtree.memoryOptimizationThreshold",
+      "Threshold for heap memory at which optimization of tree map memory usage starts. Optimize tree map when level of free memory less than X%",
+      Long.class, 80L),
+
   MVRBTREE_ENTRY_KEYS_IN_MEMORY("mvrbtree.entryKeysInMemory", "Keep unserialized keys in memory", Boolean.class, Boolean.FALSE),
 
   MVRBTREE_ENTRY_VALUES_IN_MEMORY("mvrbtree.entryValuesInMemory", "Keep unserialized values in memory", Boolean.class,
Index: tests/src/test/java/com/orientechnologies/orient/test/database/auto/OutOfBoundsTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- tests/src/test/java/com/orientechnologies/orient/test/database/auto/OutOfBoundsTest.java	(revision )
+++ tests/src/test/java/com/orientechnologies/orient/test/database/auto/OutOfBoundsTest.java	(revision )
@@ -0,0 +1,66 @@
+package com.orientechnologies.orient.test.database.auto;
+
+import com.orientechnologies.orient.core.db.document.ODatabaseDocumentPool;
+import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
+import com.orientechnologies.orient.core.metadata.schema.OClass;
+import com.orientechnologies.orient.core.metadata.schema.OSchema;
+import com.orientechnologies.orient.core.metadata.schema.OType;
+import com.orientechnologies.orient.core.record.impl.ODocument;
+import org.testng.annotations.Test;
+
+/**
+ * @author EniSh
+ *         Date: 09.05.12
+ */
+public class OutOfBoundsTest {
+    @Test
+    public void test() throws Exception {
+//        new ODatabaseDocumentTx("local:test2").create();
+        ODatabaseDocumentTx db = ODatabaseDocumentPool.global().acquire("remote:localhost/test2", "admin", "admin");
+//        ODatabaseDocumentTx db = ODatabaseDocumentPool.global().acquire("local:test2", "admin", "admin");
+
+        OSchema schema = db.getMetadata().getSchema();
+        if(!schema.existsClass("Customer"))
+        {
+            OClass oc = schema.createClass("Customer");
+            oc.createProperty("name", OType.STRING);
+
+            oc = schema.createClass("Invoice");
+            oc.createProperty("customer", OType.LINK);
+            oc.createProperty("name", OType.STRING);
+            oc.createProperty("number", OType.INTEGER);
+
+            // this is to simulate a LOT of many to one relations
+            // dodn't want to use set to keep it all in memory
+            oc.createIndex("idx_customer", OClass.INDEX_TYPE.NOTUNIQUE, "customer");
+
+            schema.save();
+
+            StringBuilder buff = new StringBuilder();
+            for(int i=0; i<=5; i++)
+            {
+                ODocument customer = db.newInstance("Customer");
+                buff.setLength(0);
+                buff.append("Customer ").append(i);
+                customer.field("name",buff.toString());
+                db.save(customer);
+                System.out.println(buff.toString());
+
+                for(int j=0; j<200000; j++)
+                {
+                    ODocument invoice = new ODocument("Invoice");
+                    invoice.field("customer",customer);
+                    buff.setLength(0);
+                    buff.append(i).append('-').append(j);
+                    invoice.field("name",buff.toString());
+                    invoice.field("number",j);
+                    if(j%1000 == 0)
+                    {
+                        System.out.println(buff.toString());
+                    }
+                    db.save(invoice);
+                }
+            }
+        }
+    }
+}
Index: build.number
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- build.number	(revision 5536)
+++ build.number	(revision )
@@ -1,3 +1,3 @@
 #Build Number for ANT. Do not edit!
-#Wed May 09 01:05:12 CEST 2012
-build.number=11892
+#Wed May 09 18:42:10 EEST 2012
+build.number=11895
