Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Android SQLite cursor load all records into memory at once?

Tags:

android

sqlite

Does an Android SQLite cursor load all data for a query into memory, or is there some sort of optimization strategy that's part of its implementation?

like image 386
Julian A. Avatar asked Jul 16 '15 21:07

Julian A.


People also ask

What does cursor do in Android studio?

The basic purpose of a cursor is to point to a single row of the result fetched by the query. We load the row pointed by the cursor object. By using cursor we can save lot of ram and memory. Here, we pass the table name, column name only then we receive the cursor.

Which function is used for moving the cursor to the specified row in Android?

moveToNext(): Moves the cursor to the next row relative to the current position. boolean Cursor.

What is the use of cursor in sqlite database?

A Cursor implementation that exposes results from a query on a SQLiteDatabase . SQLiteCursor is not internally synchronized so code using a SQLiteCursor from multiple threads should perform its own synchronization when using the SQLiteCursor.

Which method is used to retrieve data from sqlite of Android?

We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data.


2 Answers

A SQLiteCursor fills a "window" with data as you navigate through it. My recollection is that the window size is 1MB, but I can't point you to specific code that backs up that recollection. So, for small queries, the net effect is that the SQLiteCursor will hold the entire result set in memory, once you start accessing rows and columns.

like image 100
CommonsWare Avatar answered Sep 21 '22 15:09

CommonsWare


Thanks for CommonsWare about Window term so I reversed again Android by navigating those classes SQLiteCursor -> AbstractWindowedCursor -> CursorWindow . Here is CursorWindow constructor:

 public CursorWindow(String name) {
        mStartPos = 0;
        mName = name != null && name.length() != 0 ? name : "<unnamed>";
        if (sCursorWindowSize < 0) {
            /** The cursor window size. resource xml file specifies the value in kB.
             * convert it to bytes here by multiplying with 1024.
             */
            sCursorWindowSize = Resources.getSystem().getInteger(
                com.android.internal.R.integer.config_cursorWindowSize) * 1024;
        }
        mWindowPtr = nativeCreate(mName, sCursorWindowSize);
        if (mWindowPtr == 0) {
            throw new CursorWindowAllocationException("Cursor window allocation of " +
                    (sCursorWindowSize / 1024) + " kb failed. " + printStats());
        }
        mCloseGuard.open("close");
        recordNewWindow(Binder.getCallingPid(), mWindowPtr);
    }

As you can see, sCursorWindowSize is the size that CommonsWare has mentioned:

sCursorWindowSize = Resources.getSystem().getInteger(
                com.android.internal.R.integer.config_cursorWindowSize) * 1024;

As my current version is Android SDK 23.0.1, the value of com.android.internal.R.integer.config_cursorWindowSize is 2048. It means 2MB. I don't have another version SDK for checking.

like image 42
hqt Avatar answered Sep 23 '22 15:09

hqt