Memory Management When Using Sqlite Database In Iphone
Solution 1:
Sqlite uses a cache for requests. Close & reopen the database from time to release cache memory.
You shouldnt care unless your memory requirements are high.
You can catch critical conditions in the UIApplicationDelegate method applicationDidReceiveMemoryWarning or UIViewController delegate method didReceiveMemoryWarning
If one of these methods is called, close & reopen the database.
Solution 2:
I've not seen any memory leaks caused by sqlite. It does use a reasonable chunk of memory, but think of how much code you'd need to write and data you'd need to cache to do the same thing...
The best advice is to use efficient SQL and reset your statement handles as soon as possible. Finalizing your prepared statements might also help, though I've not found the need to do that.
People often recommend periodically closing and reopening the database. While this won't hurt I've not seen any practical benefit myself.
Finally, on the sqlite website you'll see talk of functions to manage memory. These sound quite seductive until you realise they're optional and are not enabled in the default build on the iPhone.
Solution 3:
There are also PRAGMAs to modify the cache size.
Solution 4:
I've had memory usage shoot up in SQLite when doing many INSERTs (> 1000) in a row. Write performance was also slow. These issues were almost completely eliminated by wrapping the loop doing the INSERTs in a transaction. I posted some sample code for this in response to this question.
Solution 5:
You really want to limit the cache size of sqlite in iphone applications. When you launch your application and initialize your database run a command like:
constchar *pragmaSql = "PRAGMA cache_size = 50";
if (sqlite3_exec(database, pragmaSql, NULL, NULL, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to execute pragma statement with message '%s'.", sqlite3_errmsg(database));
}
this will prevent sqlite from caching your queries and slowly consuming all of your memory.
Post a Comment for "Memory Management When Using Sqlite Database In Iphone"