Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed even after closing cursor
    primarykey
    data
    text
    <p>There are many questions on SO about CursorWindowAllocatoinException:</p> <ul> <li><a href="https://stackoverflow.com/questions/11340257/sqlite-android-database-cursor-window-allocation-of-2048-kb-failed">SQLite Android Database Cursor window allocation of 2048 kb failed</a></li> <li><a href="https://stackoverflow.com/questions/17495713/could-not-allocate-cursorwindow">Could not allocate CursorWindow</a></li> <li><a href="https://stackoverflow.com/questions/12577400/out-of-memory-when-allocating-cursors">Out of Memory when allocating cursors</a></li> <li><a href="https://stackoverflow.com/questions/12029437/android-mysqlite-cursorwindowallocationexception-crash">Android SQLite CursorWindowAllocationException crash</a></li> </ul> <p>All of them suggest that cursor must be closed after use. But that did not resolve my problem. Here is my code:</p> <pre><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 &gt; 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(); } </code></pre> <p>I run this method every few seconds. After half an hour, I get <code>CursorWindowAllocationException</code> in <code>int numRows=cur.getCount();</code> and then my service stops. </p> <p>Since I am closing the cursor, there may be a problem with my Helper class. Here is the code:</p> <pre><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); } } </code></pre> <p>How can I prevent this exception from being thrown?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload