光标窗口已满 - 内存不足

时间:2015-05-24 16:18:45

标签: android database cursor

我正在获取光标窗口已满错误。我花了很长时间看待提出的类似问题和文档。我希望我的两个db helper类方法试图让游标做太多。

我的UI线程中的一个方法是调用我的db帮助器方法,因此我将其更改为AsyncTask - 但没有更改。然后我认为这是因为我在使用后没有关闭我的游标 - 所以我添加了对finally语句的关闭 - 但仍然没有改变 - 我也关闭了数据库以防万一正在影响。

我的两个db帮助方法:

public List<List<String>> getAllAnswersByQuestion1() {
    List<String> array1 = new ArrayList<String>();    
    List<String> array2 = new ArrayList<String>();  

    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< List<String> > listArray = new ArrayList< List<String> >();
   listArray.add(array1);
   listArray.add(array2);

   return listArray;

} finally {
    if (c!=null) {
        c.close();

    if (db != null)
        db.close();
    }
}

}

public List<List<String>> getAnswers(String answerId) {


    List<String> array1 = new ArrayList<String>();    
    List<String> array2 = new ArrayList<String>();  

    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< List<String> > listArray2 = new ArrayList< List<String> >();
   listArray2.add(array1);
   listArray2.add(array2);

   return listArray2;

    } finally {
        if (c!=null) {
            c.close();

        if (db != null)
            db.close();
        }

}
}

我还尝试了一些代码来帮助识别内存泄漏,但它似乎没有用。这是我的logcat:

05-24 16:56:34.729: D/dalvikvm(26305): GC_FOR_ALLOC freed <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 <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 <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.<init>(Throwable.java:85)
05-24 16:56:53.039: I/dalvikvm(26305):   at java.lang.Error.<init>(Error.java:37)
05-24 16:56:53.039: I/dalvikvm(26305):   at java.lang.VirtualMachineError.<init>(VirtualMachineError.java:35)
05-24 16:56:53.039: I/dalvikvm(26305):   at java.lang.OutOfMemoryError.<init>(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)

不幸的是,我无法根据此logcat检测出如何最好地改进我的方法。真正能够指向正确的方向。非常感谢。

0 个答案:

没有答案