1

I am working on an application in which I have some info stored in the database. And I am getting new info as well in my service but before committing them to my database, I want to check if the new info I have already exists in the database or not. For that, I am querying the database for that particular info from my service and I got android.database.CursorWindowAllocationException.. I googled it and found this answer. According to that, I am getting that error because I am querying the database from my service.

Any workaround to this?

CODE TO QUERY DATABASE

public String getLogData() {
    String[] coloumns = { KEY_NETWORK_INFO };
    Cursor c = ourDatabase.query(LOG_TABLE_NAME, coloumns, null, null,
            null, null, null);
    String result = "";
    int iNetworkInfo = c.getColumnIndex(KEY_NETWORK_INFO);
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        result = result + c.getString(iNetworkInfo) + ", ";
    }
    return result;
}

LOGCAT:

    01-21 13:43:00.633: E/AndroidRuntime(14871): android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=762 (# cursors opened by this proc=762)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at android.database.CursorWindow.<init>(CursorWindow.java:108)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:139)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at com.iqra.adeel.newfyp.part1.AreaDatabase.getLogData(AreaDatabase.java:153)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at com.iqra.adeel.newfyp.part1.MyService$MyPhoneStateListener.onSignalStrengthsChanged(MyService.java:102)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at android.telephony.PhoneStateListener$1.handleMessage(PhoneStateListener.java:301)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at android.os.Looper.loop(Looper.java:135)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at android.app.ActivityThread.main(ActivityThread.java:5223)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at java.lang.reflect.Method.invoke(Native Method)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at java.lang.reflect.Method.invoke(Method.java:372)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-21 13:43:00.633: E/AndroidRuntime(14871):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Community
  • 1
  • 1
Adeel Shahzad
  • 188
  • 1
  • 16
  • possible duplicate of [SQLite Android Database Cursor window allocation of 2048 kb failed](http://stackoverflow.com/questions/11340257/sqlite-android-database-cursor-window-allocation-of-2048-kb-failed) – Muhammed Refaat Jan 21 '15 at 09:14

3 Answers3

3

This error is nearly always due to not closing a cursor when it's finished with.And you call getLogData from service so if cursors are not closed and an application continues to open new ones, this error is likely to occur.see here

So in your code close cursor as

 if (c != null)
     c.close();

before returning result,i.e. rewrite getLogData method as

public String getLogData() {
    String[] coloumns = { KEY_NETWORK_INFO };
    Cursor c = ourDatabase.query(LOG_TABLE_NAME, coloumns, null, null,
            null, null, null);
    String result = "";
    int iNetworkInfo = c.getColumnIndex(KEY_NETWORK_INFO);
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        result = result + c.getString(iNetworkInfo) + ", ";
    }
    if (c != null)
     c.close();
    return result;
}
Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
1

I think this problem occurs because you don't close your Cursor object. You should close it in the Service once you're done making use of it and the issue won't persist.

Rishabh
  • 1,901
  • 2
  • 19
  • 18
1

It is because of cursor is not closed once the task is finished for the cursor. Cursor required a memory whenever you open the cursor and it will tke more memory if you are not closing it. And if you check the error

"Open Cursors=762 (# cursors opened by this proc=762)"

means open cursors in your code is 762 and for all it will required memory. So close cursor whenever its not required.

like in your case particular for the method which u hve posted as a code in tht close it after looping for data

if (c != null)
     c.close();
user1140237
  • 5,015
  • 1
  • 28
  • 56