title: Out of Memory when allocating cursors
body: <p>I have a memory problem that I can't figure out. I have one class that does all my database retrieving work. The error I have is the following:</p>
  
  <pre><code>android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=733 (# cursors opened by this proc=733)
  </code></pre>
  
  <p>The memory allocation error occurs when I do this:</p>
  
  <pre><code>mDatabaseInterface.getGraphForLevel(level);
  </code></pre>
  
  <p>I know it's a leak because I call this method every 2.5 seconds roughly, and the 5 or 6 first calls go through easily. Now here are the methods in my DatabaseInterface class:</p>
  
  <pre><code>public Graph getGraphForLevel(Level level) {
  
      //get the nodes
      ArrayList&lt;Node&gt; nodes = new ArrayList&lt;Node&gt;(Arrays.asList(this.getNodesWithLevel(level)));
      //get the edges
      ArrayList&lt;Edge&gt; edges = new ArrayList&lt;Edge&gt;(Arrays.asList(this.getEdgesWithNodes(nodes)));
  
      return new Graph(nodes, edges);
  }
  
  public Node[] getNodesWithLevel(Level level) {
  
      List&lt;Node&gt; l = new ArrayList&lt;Node&gt;();
  
      Cursor cursor = mDatabase.query("nodes", null, 
              "level = " + wrapSql(String.valueOf(level.getId())), null, null, null, null);
  
      while (cursor.moveToNext()) {
          l.add(parseNodeFromCursor(cursor));
      }
  
      cursor.close();
  
      return l.toArray(new Node[l.size()]);       
  }
  
  private Node parseNodeFromCursor(Cursor cursor) {
  
      Level l = getLevelWithId(cursor.getInt(2));
  
      return new Node(cursor.getInt(0), cursor.getString(1), l, 
              cursor.getInt(4), cursor.getInt(5));
  }
  </code></pre>
  
  <p>I have a lot of methods that call each other but I know it's not a recursion problem because this class works in another app. My main question is why doesn't <code>cursor.close()</code> liberate the cursor? If I do something like:</p>
  
  <pre><code>cursor = mDatabase.query(...);
  cursor.moveToNext();
  Node node = new Node(cursor.getInt());
  cursor.close();
  </code></pre>
  
  <p>Is the cursor retained in that case?</p>
  
  <p>Thanks in advance.</p>