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?
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.
moveToNext(): Moves the cursor to the next row relative to the current position. boolean Cursor.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With