Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android disadvantages of not closing cursor

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.

like image 987
Haris Avatar asked Aug 09 '12 21:08

Haris


People also ask

Do I need to close cursor Android?

Yes, it's recommended to close the cursor when you are done using that cursor object so that cursor can do whatever house keeping work it wants to do upon closure.

What would be result of not closing cursor?

Not closing a cursor will keep locks active that it holds on the rows where it is positioned.

Which method is used to close the cursor in Android?

To close the cursor, call the Cursor. close() method. Note that if you close a database that has cursors open in it, then it will throw an exception and close any open cursors for you. For best results, close your cursors from within a finally block.


2 Answers

Closing a Cursor will avoid any potential memory leaks, so YES they should always be closed when no longer used.

like image 132
Tyler Treat Avatar answered Oct 02 '22 04:10

Tyler Treat


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.

like image 40
you786 Avatar answered Oct 02 '22 05:10

you786