While perform CBLQuery run operation with a document count above 20,000, application got crashed due to memory issue

query

#1

While performing the query operation, I’m performing a memory issue and OS terminate the application. Limiting the processing count up 20,000 works fine. But if anything other than that makes the problem.

Couchbase verison
pod ‘couchbase-lite-ios/SQLCipher’, ‘1.4.1’

Actually, I’m looking into existing code to resolve this issue. And am a bit new to couchbase. Below given the piece of code causing the issue.

let database = try Couchbase.openDatabase(databaseName: databaseName)
database.manager.backgroundTellDatabaseNamed(database.name) { database in
guard let query = database.existingViewNamed(viewName)?.createQuery() else {
callback(nil)
return
}
query.prefetch = true
do {
// query.limit = 20000
let documents = try query.run().compactMap({$0 as? CBLQueryRow}).compactMap({$0.document}) // Here we are watching the issue
callback(documents)
} catch {
callback(nil)
}

If anyone has this experience, please share and that would be very helpful.


#2

Well yes, you’re loading all query results and all their referenced documents into memory at once; I can see how that would run you out of memory with a large enough database!

Instead of doing this, you should iterate over the query results and process each one during the iteration. That way only one query result needs to be in memory at once.

Also, it’s inefficient to use the document property to load the document from a query result – it’s a second database access. You should instead have your map function emit the properties you’ll need in its value, so you can access them right from the query result.