我的 Realm 增长如此之快,所以我对内存分配大小有一个异常(exception)。我的 Realm 大小约为 3.2 GB,因此我将此 Realm 的配置用于解决此问题:
let config = Realm.Configuration(
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
schemaVersion: 11,
// Set the block which will be called automatically when opening a Realm with
// a schema version lower than the one set above
migrationBlock: { migration, oldSchemaVersion in
},shouldCompactOnLaunch: {totalBytes, usedBytes -> Bool in
guard totalBytes > 10 * 1024 * 1024 * 1024 else { return false }
guard Double(usedBytes) / Double(totalBytes) < 0.5 else { return false }
print("Should compact Realm database: \(usedBytes) / \(totalBytes)")
return true
})
Realm.Configuration.defaultConfiguration = config
但是,问题仍然存在。我做错了什么?
问候
最佳答案
我的猜测是您在 dispatch async
方法中缺少 autoreleasepool
。
documentation表明您应该这样做。
// Query and update from any thread
DispatchQueue(label: "background").async {
autoreleasepool { // <-- !!!
let realm = try! Realm()
let theDog = realm.objects(Dog.self).filter("age == 1").first
try! realm.write {
theDog!.age = 3
}
}
}
当然,另一种可能性是您多次插入大约 100000 个对象,而不是将它们分成大约 1000 个的批处理。
另一种可能性是将每个项目插入其自己的事务中。
因此,超大交易可以分配大量空间,但逐项插入也会占用大量空间,因此是中间地带。
关于ios - mmap() 失败 : Cannot allocate memory size: 268435456 offset: 2684354560 on RealmSwift 2. 10.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46490082/