Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed even after closing cursor

There are many questions on SO about CursorWindowAllocatoinException:

  • SQLite Android Database Cursor window allocation of 2048 kb failed
  • Could not allocate CursorWindow
  • Out of Memory when allocating cursors
  • Android SQLite CursorWindowAllocationException crash

All of them suggest that cursor must be closed after use. But that did not resolve my problem. Here is my code:

String query = "select serial from tbl1 union select serial from tbl2 union select serial from tbl3";
    SQLiteDatabase db = null;
    Cursor cur = null;
    try {
        SettingsDatabaseHelper dal = new SettingsDatabaseHelper(
                c);
        db = dal.getReadableDatabase();
        cur = db.rawQuery(query, null);
        int numRows = cur.getCount();
        if (numRows > 0) {
            cur.moveToFirst();

            int serialIdx = cur.getColumnIndexOrThrow("serial");
            for (boolean hasItem = cur.moveToFirst(); hasItem; hasItem = cur
                    .moveToNext()) {

                String serial = cur.getString(serialIdx);
                if (Validator.getInstance().isValidSerial(serial))
                    serials.add(serial);

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

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

I run this method every few seconds. After half an hour, I get CursorWindowAllocationException in int numRows=cur.getCount(); and then my service stops.

Since I am closing the cursor, there may be a problem with my Helper class. Here is the code:

public class SettingsDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "mydb.db";
    private static final int DATABASE_VERSION = 1;


    private static final String TBL_CREATE="create table...";



    public SettingsDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(TBL_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onCreate(db);
    }
}

How can I prevent this exception from being thrown?

like image 756
danrah Avatar asked Nov 05 '13 14:11

danrah


1 Answers

I was querying the database from a service. When I did it from an activity, the problem never occurred again.

Alongside querying, I was reading from the serial port within the same service. Maybe that caused the problem. However, when I used an activity instead of the service, the problem never happened again.

like image 171
danrah Avatar answered Nov 16 '22 19:11

danrah