I have some questions regarding Cursor
.
I want to know what are the disadvantages of not closing a Cursor
?
It deallocates resources, but what resources are deallocated?
If we do not close the Cursor
? What will be the consequences and to what extent?
Not closing a Cursor
does not affect Activity
a bit, but it gives error in a log cat.
Closing a Cursor
will avoid any potential memory leaks, so YES they should always be closed when no longer used.
Cursor is only an interface. A lot depends on the implementation, what specific class that implements that interface you are using...
In the case of SQLiteCursor, there are quite a few things getting cleaned-up (this is from Froyo):
@Override
public void close() {
super.close();
deactivateCommon();
mQuery.close();
mDriver.cursorClosed();
}
Not closing your Cursor, the data should still be consistent, but you are going to leak memory...
I don't think you'll see many issues from forgetting to close one cursor. The issue (and this applies to almost any memory leak) is that if you keep doing it over and over again, eventually something bad will happen.
For example, I wrote an app that uploads data from a device's SQLite DB to a server. If I didn't close the cursor each time I was done reading and writing from the database, eventually I would run into out-of-memory errors and various other issues.
If you do not close the cursor then you will have issues accessing your data in your database because while the database is open, it is not accessible should something happen like the Activity is paused or otherwise. Specifically, this problem can be alleviated if you let the Activity manage it.