diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift index 02fa87682..4bf67794e 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift @@ -17,6 +17,22 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele return true } + + func applicationDidReceiveMemoryWarning(application: UIApplication) { + // Release as much memory as possible. + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) { + dbQueue.releaseMemory() + } + } + + func applicationDidEnterBackground(application: UIApplication) { + let task = application.beginBackgroundTaskWithExpirationHandler(nil) + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { + // Release as much memory as possible. + dbQueue.releaseMemory() + application.endBackgroundTask(task) + } + } // MARK: - Split view diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index f921da767..145ac3f85 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -85,8 +85,8 @@ public final class DatabasePool { // // See DatabasePoolTests.testTwoReadsWithMiddleWrite(). - return try readerPool.get { serializedDatabase in - try serializedDatabase.inDatabase { db in + return try readerPool.get { reader in + try reader.inDatabase { db in var result: T? = nil try db.inTransaction(.Deferred) { result = try block(db: db) @@ -162,29 +162,25 @@ public final class DatabasePool { } } - /// Eventually closes all reader connections. - /// - /// Unused reader connections are immediately closed. Currently used reader - /// connections will not be reused, and will eventually be closed. - public func closeReaderConnections() { - readerPool.clear() - } - // MARK: - Memory management /// Free as much memory as possible. /// - /// See also closeReaderConnections() + /// This method blocks the current thread until all database accesses are completed. + /// (TODO: test this assertion, and also that all readers connections are closed). public func releaseMemory() { writer.inDatabase { db in db.releaseMemory() } - readerPool.forEach { serialiazedDatabase in - serialiazedDatabase.inDatabase { db in + + readerPool.forEach { reader in + reader.inDatabase { db in db.releaseMemory() } } + + readerPool.clear() } diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index b4bb12126..9fcb45bab 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -91,6 +91,8 @@ public final class DatabaseQueue { // MARK: - Memory management /// Free as much memory as possible. + /// + /// This method blocks the current thread until all database accesses are completed. public func releaseMemory() { serializedDatabase.inDatabase { db in db.releaseMemory() diff --git a/README.md b/README.md index a98e20f74..b6c9d6f43 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,7 @@ try dbQueue.inDatabase { db in - [String Comparison](#string-comparison) - [Transactions](#transactions) - [Error Handling](#error-handling) +- [Memory Management](#memory-management) - Advanced topics: - [Custom Value Types](#custom-value-types) - [Prepared Statements](#prepared-statements) @@ -286,7 +287,7 @@ import GRDB let dbPool = try DatabasePool(path: "/path/to/database.sqlite") ``` -Based on [SQLite WAL Mode](https://www.sqlite.org/wal.html), pools grant you with multiple readers, and one writer. Only the writer is allowed to modify the database. Readers can run in parallel with other readers, and with the writer. +Based on [SQLite WAL Mode](https://www.sqlite.org/wal.html), a database pool allows concurrent reading and writing. Writes are serialized, so that there is no conflict. Reads are isolated, which means that you can perform several fetch requests in a row without being affected by eventual concurrent writes. > :point_up: **Note**: your application should have a unique instance of DatabasePool connected to a database file. You may experience concurrency trouble if you do otherwise. > @@ -307,7 +308,7 @@ try dbPool.writeInTransaction { db in } ``` -The `read` method also blocks the current thread until your database fetches are done. Database modifications are not allowed: +The `read` method also blocks the current thread until your database fetches are done. Your application threads can perform concurrent reads. Database modifications are not allowed: ```swift dbPool.read { db in @@ -325,23 +326,23 @@ let wineCount = dbPool.read { db in print(wineCount) ``` -**Readers are isolated from the writer.** The modifications performed by the writer are not visible inside a `read`: +**Reads are isolated from writes:** database updates are not visible inside a `read` block: ```swift dbPool.read { db in - // Those two values are guaranteed to be equal, even if the writer modifies - // the wines table between the two requests: + // Those two values are guaranteed to be equal, even if the `wines` + // tables is modified between the two requests: let count1 = Int.fetchOne(db, "SELECT COUNT(*) FROM wines") let count2 = Int.fetchOne(db, "SELECT COUNT(*) FROM wines") } dbPool.read { db in - // Now this one may be different: + // Now this value may be different: let count = Int.fetchOne(db, "SELECT COUNT(*) FROM wines") } ``` -The total number of concurrent readers is limited. When all readers are busy, the `read` method blocks until a reader turns available. +**The total number of concurrent reads is limited.** When the maximum number has been reached, the `read` method blocks until another read has completed. **You can configure database pools:** @@ -359,7 +360,10 @@ let dbPool = try DatabasePool( See [Configuration](http://cocoadocs.org/docsets/GRDB.swift/0.47.0/Structs/Configuration.html) and [Concurrency](#concurrency) for more details. -> :warning: **Warning**: database pools, although well tested, are still experimental. In particular, unused readers are not released. The memory pressure of multiple database connections is unknown. The best number of readers is unknown. Please share the results of your experiments! + +Database pools are more memory-hungry than database queues. See [Memory Management](#memory-management) for more information. + +> :warning: **Warning**: database pools, although well tested, are still experimental. In particular, the best number of concurrent reads is unknown. Please share the results of your experiments! ## Executing Updates @@ -1144,6 +1148,58 @@ The `fromDatabaseValue()` factory method returns an instance of your custom type As an example, see [DatabaseTimestamp.playground](Playgrounds/DatabaseTimestamp.playground/Contents.swift): it shows how to store dates as timestamps, unlike the built-in [NSDate](#nsdate-and-nsdatecomponents). +## Memory Management + +**You can reclaim memory used by GRDB.** + +The most obvious way is to release your [database queues](#database-queues) and [pools](#database-pools): + +```swift +// Eventually release all memory, after all database accesses are completed: +dbQueue = nil +dbPool = nil +``` + +Yet both SQLite and GRDB use non-essential memory that help them perform better. You can claim this memory with the `releaseMemory` method: + +```swift +// Release as much memory as possible. +dbQueue.releaseMemory() +dbPool.releaseMemory() +``` + +This method blocks the current thread until all current database accesses are completed, and the memory collected. + + +### Memory Management on iOS + +**The iOS operating system likes applications that do not consume much memory.** + +You should call the `releaseMemory` method when your application receives a memory warning, and when it enters background. Since `releaseMemory` is blocking, make sure you dispatch it to some background queue. + +For example, assuming a global `dbQueue`: + +```swift +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + + func applicationDidReceiveMemoryWarning(application: UIApplication) { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) { + dbQueue.releaseMemory() + } + } + + func applicationDidEnterBackground(application: UIApplication) { + let task = application.beginBackgroundTaskWithExpirationHandler(nil) + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { + dbQueue.releaseMemory() + application.endBackgroundTask(task) + } + } +} +``` + + ## Prepared Statements **Prepared Statements** let you prepare an SQL query and execute it later, several times if you need, with different arguments. diff --git a/TODO.md b/TODO.md index df73ec090..ce7b3cffa 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,3 @@ -- [ ] document releaseMemory() -- [ ] Rewrite DatabasePool documentation without the "reader" and "writer" words. - [ ] Query builder - [ ] SELECT readers.*, books.* FROM ... JOIN ... - [ ] date functions