BlackBerry: Cursor.close() and memory optimization

There is a couple of things I’ve learned about BlackBerry Java development that I think might be worth sharing.

1. SQLite: Don’t use Cursor.close() – use Statement.close() instead.

It turned out that Cursor.close() method is also closing the Statement that was used to create that Cursor, even if the Statement was later reinitialized. I believe that removing Cursor.close() calls from the code helped me to avoid a couple of ‘File System Error (12)’ errors while doing some IO operations over the SQLite db file. I tend to think that ‘File System Error (12)’ error actually means ‘File is locked’ (thank you BlackBerry, btw, for such nice, self-explanatory error messages and providing no documentation about them), so now it all makes sense to me.

More info: http://supportforums.blackberry.com/t5/Java-Development/Cursor-close-closes-the-Statement-used-to-Create-It/ta-p/614699

2. Memory optimization

There is a nice blog post that provides an overview of RIM’s memory monitoring tools (Memory Statistics, Object Statistics and Objects) and gives a set of steps that should be taken in order to detect a memory leak and identify the root cause. I’ve seen this article before, but this time I’ve actually read it thoroughly (all three parts) and I think it’s very good place to start if you do a memory optimization:

http://devblog.blackberry.com/2009/10/how-to-find-that-memory-leak-part-one/

http://devblog.blackberry.com/2010/01/how-to-find-that-memory-leak-part-two-detecting-the-leak/ (you might need to tweak images’ source URLs in order to get them for this part)

http://devblog.blackberry.com/2010/03/how-to-find-that-memory-leak-part-three/