SQLite Android Database Cursor window allocation of 2048 kb failed SQLite Android Database Cursor window allocation of 2048 kb failed android android

SQLite Android Database Cursor window allocation of 2048 kb failed


Most often the cause for this error are non closed cursors. Make sure you close all cursors after using them (even in the case of an error).

Cursor cursor = null;try {    cursor = db.query(...    // do some work with the cursor here.} finally {    // this gets called even if there is an exception somewhere above    if(cursor != null)        cursor.close();}

To make your App crash when you are not closing a cursor you can enable Strict Mode with detectLeakedSqlLiteObjects in your Applications onCreate:

StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()   .detectLeakedClosableObjects()   .detectLeakedSqlLiteObjects()   .penaltyDeath()   .penaltyLog()   .build();StrictMode.setVmPolicy(policy);

Obviously you would only enable this for debug builds.


If you're having to dig through a significant amount of SQL code you may be able to speed up your debugging by putting the following code snippet in your MainActivity to enable StrictMode. If leaked database objects are detected then your app will now crash with log info highlighting exactly where your leak is. This helped me locate a rogue cursor in a matter of minutes.

@Overrideprotected void onCreate(Bundle savedInstanceState) {   if (BuildConfig.DEBUG) {              StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()         .detectLeakedSqlLiteObjects()         .detectLeakedClosableObjects()         .penaltyLog()         .penaltyDeath()         .build());    }    super.onCreate(savedInstanceState);    ...    ...


I have just experienced this issue - and the the suggested answer of not closing the cursor while valid, was not how I fixed it. My issue was closing the database when SQLite was trying to repopulate it's cursor. I would open the database, query the database to get a cursor to a data set, close the database and iterate over the cursor. I noticed whenever I hit a certain record in that cursor, my app would crash with this same error in OP.

I assume that for the cursor to access certain records, it needs to re-query the database and if it is closed, it will throw this error. I fixed it by not closing the database until I had completed all the work I needed.