You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ka...@apache.org on 2006/10/25 09:44:56 UTC

svn commit: r467578 - /db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/RAMTransaction.java

Author: kahatlen
Date: Wed Oct 25 00:44:55 2006
New Revision: 467578

URL: http://svn.apache.org/viewvc?view=rev&rev=467578
Log:
DERBY-912: OutOfMemory error on continuous execution of SQL statement

Reuse free sort identifiers.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/RAMTransaction.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/RAMTransaction.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/RAMTransaction.java?view=diff&rev=467578&r1=467577&r2=467578
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/RAMTransaction.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/RAMTransaction.java Wed Oct 25 00:44:55 2006
@@ -21,12 +21,14 @@
 
 package org.apache.derby.impl.store.access;
 
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Properties;
 import java.util.Vector;
 
 import org.apache.derby.iapi.reference.SQLState;
+import org.apache.derby.iapi.util.ReuseFactory;
 
 import org.apache.derby.iapi.services.context.ContextManager;
 
@@ -120,6 +122,11 @@
 	private Vector sorts;
 	private Vector sortControllers;
 
+    /** List of sort identifiers (represented as <code>Integer</code> objects)
+     * which can be reused. Since sort identifiers are used as array indexes,
+     * we need to reuse them to avoid leaking memory (DERBY-912). */
+    private ArrayList freeSortIds;
+
 	/**
 	Where to look for temporary conglomerates.
 	**/
@@ -163,6 +170,7 @@
 		conglomerateControllers = new Vector();
 
 		sorts                   = null; // allocated on demand.
+		freeSortIds             = null; // allocated on demand.
 		sortControllers         = null; // allocated on demand
 
         if (parent_tran != null)
@@ -314,6 +322,7 @@
                         sort.drop(this);
                 }
                 sorts.removeAllElements();
+                freeSortIds.clear();
             }
 		}
 	}
@@ -1717,10 +1726,22 @@
                         estimatedRowSize);
 
 		// Add the sort to the sorts vector
-		if (sorts == null)
+		if (sorts == null) {
 			sorts = new Vector();
-		long sortid = sorts.size();
-		sorts.addElement(sort);
+            freeSortIds = new ArrayList();
+        }
+
+        int sortid;
+        if (freeSortIds.isEmpty()) {
+            // no free identifiers, add sort at the end
+            sortid = sorts.size();
+            sorts.addElement(sort);
+        } else {
+            // reuse a sort identifier
+            sortid = ((Integer) freeSortIds.remove(freeSortIds.size() - 1))
+                .intValue();
+            sorts.setElementAt(sort, sortid);
+        }
 
 		return sortid;
 	}
@@ -1748,6 +1769,7 @@
         {
             sort.drop(this);
             sorts.setElementAt(null, (int) sortid);
+            freeSortIds.add(ReuseFactory.getInteger((int) sortid));
         }
     }