ios - mmap() 失败 : Cannot allocate memory size: 268435456 offset: 2684354560 on RealmSwift 2. 10.2

标签 ios swift realm xcode9

我的 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/

相关文章:

iphone - 如何滚动包含 UIView 的 UIScrollView?

objective-c - 从相机胶卷创建一个 UIImages 数组

ios - Delphi XE7 有 iOS SDK8.3 - 无法让它使用 SDK7.1

ios - 适用于不同 iOS 设备/模拟器的 Xib?

ios - 如何使用 Realm Swift 保存值类型为 Measurement<UnitType> 的变量?

swift - 如何使用 SwiftUI 的 onDelete() 函数访问数组位置

ios - RxSwift 网络状态可观察

ios - 在 iOS 中截取带有标签和透明背景的 View 并上传到服务器

android - FindFirst 返回 null

ios - 使用*作为Realm中 'LIKE'的常规字符过滤对象