Skip to content

Commit e1a63a4

Browse files
committed
Introduce DatabasePool.releaseMemoryEventually
1 parent caab403 commit e1a63a4

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

GRDB/Core/DatabasePool.swift

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,42 @@ extension DatabasePool {
185185

186186
// MARK: - Memory management
187187

188-
/// Free as much memory as possible.
188+
/// Frees as much memory as possible, by disposing non-essential memory from
189+
/// the writer connection, and closing all reader connections.
189190
///
190-
/// This method blocks the current thread until all database accesses
191-
/// are completed.
191+
/// This method is synchronous, and blocks the current thread until all
192+
/// database accesses are completed.
193+
///
194+
/// - warning: This method can prevent concurrent reads from executing,
195+
/// until it returns. Prefer ``releaseMemoryEventually()`` if you intend
196+
/// to keep on using the database while releasing memory.
192197
public func releaseMemory() {
193198
// Release writer memory
194199
writer.sync { $0.releaseMemory() }
200+
195201
// Release readers memory by closing all connections
196202
readerPool?.barrier {
197203
readerPool?.removeAll()
198204
}
199205
}
200206

207+
/// Eventually frees as much memory as possible, by disposing non-essential
208+
/// memory from the writer connection, and closing all reader connections.
209+
///
210+
/// Unlike ``releaseMemory()``, this method does not prevent concurrent
211+
/// database accesses when it is executing. But it does not notify when
212+
/// non-essential memory has been freed.
213+
public func releaseMemoryEventually() {
214+
// Release readers memory by eventually closing all reader connections
215+
// (they will close after their current jobs have completed).
216+
readerPool?.removeAll()
217+
218+
// Release writer memory eventually.
219+
writer.async { db in
220+
db.releaseMemory()
221+
}
222+
}
223+
201224
#if os(iOS)
202225
/// Listens to UIApplicationDidEnterBackgroundNotification and
203226
/// UIApplicationDidReceiveMemoryWarningNotification in order to release

0 commit comments

Comments
 (0)