title: Cursor Window is full - out of memory
body: <p>I am getting cursor window is full error. I have spent a long time looking at similar questions raised and documentation. I expect that two of my db helper class methods are trying to get the cursor to do too much.</p>
  
  <p>One of my methods on my UI thread was calling my db helper methods, so I changed that to an AsyncTask - but no change. Then I thought it was because I wasn't closing my cursors after use - so I add closes to the finally statements - but still no change - I also closed db just in case that was impacting.</p>
  
  <p>My two db helper methods:</p>
  
  <pre><code>public List&lt;List&lt;String&gt;&gt; getAllAnswersByQuestion1() {
      List&lt;String&gt; array1 = new ArrayList&lt;String&gt;();    
      List&lt;String&gt; array2 = new ArrayList&lt;String&gt;();  
  
      array1.clear();
      array2.clear();
  
      SQLiteDatabase db = this.getReadableDatabase();
  
      String selectQuery = "SELECT  * FROM " + TABLE_ANSWERS + " ta, "
              + TABLE_QUESTION + " tq WHERE  ta." + ASID
              + " = " + "tq." + ASID;
  
     Cursor c = db.rawQuery(selectQuery, null);
  
     try {
  
     if (c.moveToNext())
     {
         do {
  
       String questdescr = c.getString(c.getColumnIndex(QDESCR));
       String answerSetIds = c.getString(c.getColumnIndex(ASID));
       array1.add(questdescr);
       array2.add(answerSetIds);
  
  
      } while (c.moveToNext());
  
   }
     List&lt; List&lt;String&gt; &gt; listArray = new ArrayList&lt; List&lt;String&gt; &gt;();
     listArray.add(array1);
     listArray.add(array2);
  
     return listArray;
  
  } finally {
      if (c!=null) {
          c.close();
  
      if (db != null)
          db.close();
      }
  }
  
  }
  
  public List&lt;List&lt;String&gt;&gt; getAnswers(String answerId) {
  
  
      List&lt;String&gt; array1 = new ArrayList&lt;String&gt;();    
      List&lt;String&gt; array2 = new ArrayList&lt;String&gt;();  
  
      array1.clear();
      array2.clear();
  
      SQLiteDatabase db = this.getReadableDatabase();
  
      String[] columns = new String[]{TDESCR, ADESCR};
      String selection = ASID+"=?";
      String[] selectionArgs = new String[]{String.valueOf(answerId)};
      Cursor c = db.query(TABLE_ANSWERS, columns, selection, selectionArgs, null, null, null);
  
      try {
  
     if (c.moveToNext())       
     {
      do {
  
       String textdescr = c.getString(c.getColumnIndex(TDESCR));
       String answersdescr = c.getString(c.getColumnIndex(ADESCR));
       array1.add(textdescr);
       array2.add(answersdescr);;
  
      } while (c.moveToNext());
  
   }
     List&lt; List&lt;String&gt; &gt; listArray2 = new ArrayList&lt; List&lt;String&gt; &gt;();
     listArray2.add(array1);
     listArray2.add(array2);
  
     return listArray2;
  
      } finally {
          if (c!=null) {
              c.close();
  
          if (db != null)
              db.close();
          }
  
  }
  }
  </code></pre>
  
  <p>I also tried a bit of code to help identify memory leaks, but it didn't seem to work. Here is my logcat:</p>
  
  <pre><code>05-24 16:56:34.729: D/dalvikvm(26305): GC_FOR_ALLOC freed &lt;1K, 1% free 86903K/87112K, paused 194ms, total 194ms
  05-24 16:56:36.689: W/CursorWindow(26305): Window is full: requested allocation 5 bytes, free space 4 bytes, window size 2097152 bytes
  05-24 16:56:38.679: W/CursorWindow(26305): Window is full: requested allocation 2 bytes, free space 0 bytes, window size 2097152 bytes
  05-24 16:56:40.789: W/CursorWindow(26305): Window is full: requested allocation 79 bytes, free space 26 bytes, window size 2097152 bytes
  05-24 16:56:42.889: W/CursorWindow(26305): Window is full: requested allocation 132 bytes, free space 22 bytes, window size 2097152 bytes
  05-24 16:56:44.949: W/CursorWindow(26305): Window is full: requested allocation 64 bytes, free space 33 bytes, window size 2097152 bytes
  05-24 16:56:47.079: W/CursorWindow(26305): Window is full: requested allocation 132 bytes, free space 81 bytes, window size 2097152 bytes
  05-24 16:56:47.309: I/dalvikvm-heap(26305): Clamp target GC heap from 101.059MB to 96.000MB
  05-24 16:56:47.309: D/dalvikvm(26305): GC_FOR_ALLOC freed 1K, 1% free 95089K/95300K, paused 222ms, total 222ms
  05-24 16:56:49.499: W/CursorWindow(26305): Window is full: requested allocation 132 bytes, free space 129 bytes, window size 2097152 bytes
  05-24 16:56:51.809: W/CursorWindow(26305): Window is full: requested allocation 79 bytes, free space 28 bytes, window size 2097152 bytes
  05-24 16:56:52.049: I/dalvikvm-heap(26305): Clamp target GC heap from 103.995MB to 96.000MB
  05-24 16:56:52.049: D/dalvikvm(26305): GC_FOR_ALLOC freed &lt;1K, 1% free 98095K/98304K, paused 224ms, total 224ms
  05-24 16:56:52.269: I/dalvikvm-heap(26305): Clamp target GC heap from 103.995MB to 96.000MB
  05-24 16:56:52.269: D/dalvikvm(26305): GC_FOR_ALLOC freed &lt;1K, 1% free 98095K/98304K, paused 224ms, total 225ms
  05-24 16:56:52.269: I/dalvikvm-heap(26305): Forcing collection of SoftReferences for 168-byte allocation
  05-24 16:56:52.529: I/dalvikvm-heap(26305): Clamp target GC heap from 103.995MB to 96.000MB
  05-24 16:56:52.529: D/dalvikvm(26305): GC_BEFORE_OOM freed 121K, 1% free 97973K/98304K, paused 260ms, total 260ms
  05-24 16:56:52.529: E/dalvikvm-heap(26305): Out of memory on a 168-byte allocation.
  05-24 16:56:52.529: I/dalvikvm(26305): "AsyncTask #2" prio=5 tid=12 RUNNABLE
  05-24 16:56:52.529: I/dalvikvm(26305):   | group="main" sCount=0 dsCount=0 obj=0x41bc65e0 self=0x603ac400
  05-24 16:56:52.529: I/dalvikvm(26305):   | sysTid=26842 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1614465112
  05-24 16:56:52.529: I/dalvikvm(26305):   | state=R schedstat=( 68056904399 9638134962 38201 ) utm=6737 stm=68 core=1
  05-24 16:56:52.529: I/dalvikvm(26305):   at android.database.CursorWindow.nativeGetString(Native Method)
  05-24 16:56:52.539: I/dalvikvm(26305):   at android.database.CursorWindow.getString(CursorWindow.java:434)
  05-24 16:56:52.539: I/dalvikvm(26305):   at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
  05-24 16:56:52.549: I/dalvikvm(26305):   at com.example.quizapp.SQLDatabaseHelper.getAllAnswersByQuestion1(SQLDatabaseHelper.java:392)
  05-24 16:56:52.559: I/dalvikvm(26305):   at com.example.quizapp.MainActivity$showNextRandomQuestion.doInBackground(MainActivity.java:394)
  05-24 16:56:52.559: I/dalvikvm(26305):   at com.example.quizapp.MainActivity$showNextRandomQuestion.doInBackground(MainActivity.java:1)
  05-24 16:56:52.559: I/dalvikvm(26305):   at android.os.AsyncTask$2.call(AsyncTask.java:288)
  05-24 16:56:52.559: I/dalvikvm(26305):   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  05-24 16:56:52.559: I/dalvikvm(26305):   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  05-24 16:56:52.559: I/dalvikvm(26305):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  05-24 16:56:52.559: I/dalvikvm(26305):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  05-24 16:56:52.559: I/dalvikvm(26305):   at java.lang.Thread.run(Thread.java:841)
  05-24 16:56:52.779: I/dalvikvm-heap(26305): Clamp target GC heap from 103.995MB to 96.000MB
  05-24 16:56:52.779: D/dalvikvm(26305): GC_FOR_ALLOC freed 0K, 1% free 97973K/98304K, paused 224ms, total 224ms
  05-24 16:56:52.779: I/dalvikvm-heap(26305): Forcing collection of SoftReferences for 112-byte allocation
  05-24 16:56:53.039: I/dalvikvm-heap(26305): Clamp target GC heap from 103.995MB to 96.000MB
  05-24 16:56:53.039: D/dalvikvm(26305): GC_BEFORE_OOM freed 0K, 1% free 97973K/98304K, paused 255ms, total 255ms
  05-24 16:56:53.039: E/dalvikvm-heap(26305): Out of memory on a 112-byte allocation.
  05-24 16:56:53.039: I/dalvikvm(26305): "AsyncTask #2" prio=5 tid=12 RUNNABLE
  05-24 16:56:53.039: I/dalvikvm(26305):   | group="main" sCount=0 dsCount=0 obj=0x41bc65e0 self=0x603ac400
  05-24 16:56:53.039: I/dalvikvm(26305):   | sysTid=26842 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1614465112
  05-24 16:56:53.039: I/dalvikvm(26305):   | state=R schedstat=( 68538814034 9650804336 38242 ) utm=6781 stm=72 core=1
  05-24 16:56:53.039: I/dalvikvm(26305):   at java.lang.Throwable.nativeFillInStackTrace(Native Method)
  05-24 16:56:53.039: I/dalvikvm(26305):   at java.lang.Throwable.fillInStackTrace(Throwable.java:166)
  05-24 16:56:53.039: I/dalvikvm(26305):   at java.lang.Throwable.&lt;init&gt;(Throwable.java:85)
  05-24 16:56:53.039: I/dalvikvm(26305):   at java.lang.Error.&lt;init&gt;(Error.java:37)
  05-24 16:56:53.039: I/dalvikvm(26305):   at java.lang.VirtualMachineError.&lt;init&gt;(VirtualMachineError.java:35)
  05-24 16:56:53.039: I/dalvikvm(26305):   at java.lang.OutOfMemoryError.&lt;init&gt;(OutOfMemoryError.java:33)
  05-24 16:56:53.039: I/dalvikvm(26305):   at android.database.CursorWindow.nativeGetString(Native Method)
  05-24 16:56:53.049: I/dalvikvm(26305):   at android.database.CursorWindow.getString(CursorWindow.java:434)
  05-24 16:56:53.049: I/dalvikvm(26305):   at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
  05-24 16:56:53.049: I/dalvikvm(26305):   at com.example.quizapp.SQLDatabaseHelper.getAllAnswersByQuestion1(SQLDatabaseHelper.java:392)
  05-24 16:56:53.049: I/dalvikvm(26305):   at com.example.quizapp.MainActivity$showNextRandomQuestion.doInBackground(MainActivity.java:394)
  05-24 16:56:53.049: I/dalvikvm(26305):   at com.example.quizapp.MainActivity$showNextRandomQuestion.doInBackground(MainActivity.java:1)
  05-24 16:56:53.049: I/dalvikvm(26305):   at android.os.AsyncTask$2.call(AsyncTask.java:288)
  05-24 16:56:53.049: I/dalvikvm(26305):   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  05-24 16:56:53.049: I/dalvikvm(26305):   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  05-24 16:56:53.049: I/dalvikvm(26305):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  05-24 16:56:53.049: I/dalvikvm(26305):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  05-24 16:56:53.049: I/dalvikvm(26305):   at java.lang.Thread.run(Thread.java:841)
  05-24 16:56:53.049: W/dalvikvm(26305): Exception thrown (Ljava/lang/OutOfMemoryError;) while throwing internal exception (Ljava/lang/OutOfMemoryError;)
  05-24 16:56:53.219: D/dalvikvm(26305): GC_FOR_ALLOC freed 77455K, 80% free 20518K/98304K, paused 169ms, total 169ms
  05-24 16:56:53.219: W/dalvikvm(26305): threadid=12: thread exiting with uncaught exception (group=0x415aad88)
  05-24 16:56:53.229: E/AndroidRuntime(26305): FATAL EXCEPTION: AsyncTask #2
  05-24 16:56:53.229: E/AndroidRuntime(26305): Process: com.example.quizapp, PID: 26305
  05-24 16:56:53.229: E/AndroidRuntime(26305): java.lang.RuntimeException: An error occured while executing doInBackground()
  05-24 16:56:53.229: E/AndroidRuntime(26305):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
  05-24 16:56:53.229: E/AndroidRuntime(26305):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
  05-24 16:56:53.229: E/AndroidRuntime(26305):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
  05-24 16:56:53.229: E/AndroidRuntime(26305):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
  05-24 16:56:53.229: E/AndroidRuntime(26305):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  05-24 16:56:53.229: E/AndroidRuntime(26305):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  05-24 16:56:53.229: E/AndroidRuntime(26305):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  05-24 16:56:53.229: E/AndroidRuntime(26305):    at java.lang.Thread.run(Thread.java:841)
  05-24 16:56:53.229: E/AndroidRuntime(26305): Caused by: java.lang.OutOfMemoryError: [memory exhausted]
  05-24 16:56:53.229: E/AndroidRuntime(26305):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Unfortunately I am not quite able to detect how best to improve my methods based on this logcat. Really could do with being pointed in the right direction. Much appreciated.</p>