Index: java/engine/org/apache/derby/impl/store/access/RAMTransaction.java
===================================================================
--- java/engine/org/apache/derby/impl/store/access/RAMTransaction.java	(revision 466194)
+++ java/engine/org/apache/derby/impl/store/access/RAMTransaction.java	(working copy)
@@ -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.
 	**/
@@ -314,6 +321,9 @@
                         sort.drop(this);
                 }
                 sorts.removeAllElements();
+                if (freeSortIds != null) {
+                    freeSortIds.clear();
+                }
             }
 		}
 	}
@@ -1719,9 +1729,19 @@
 		// Add the sort to the sorts vector
 		if (sorts == null)
 			sorts = new Vector();
-		long sortid = sorts.size();
-		sorts.addElement(sort);
 
+        int sortid;
+        if (freeSortIds == null || 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 +1768,10 @@
         {
             sort.drop(this);
             sorts.setElementAt(null, (int) sortid);
+            if (freeSortIds == null) {
+                freeSortIds = new ArrayList();
+            }
+            freeSortIds.add(ReuseFactory.getInteger((int) sortid));
         }
     }
 
