【问题标题】:mmap() failed: Cannot allocate memory size: 268435456 offset: 2684354560 on RealmSwift 2.10.2mmap() 失败:无法分配内存大小:268435456 偏移量:RealmSwift 2.10.2 上的 2684354560
【发布时间】:2017-09-29 21:58:39
【问题描述】:

我的领域发展得如此之快,所以我对内存分配大小有一个例外。我的领域大小约为 3.2 GB,所以我把这个领域的配置来解决这个问题:

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

但是,问题仍然存在。我做错了什么?

问候

【问题讨论】:

    标签: ios swift realm xcode9


    【解决方案1】:

    我猜你在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 个的批次。

    另一种可能性是在自己的事务中插入每一个项目。

    就像,超大事务可以分配大量空间,但一个一个地插入每个项目也占用大量空间,因此是中间立场。

    【讨论】:

    • 如何切割它们?我没有忘记自动释放池:/
    • 你没有忘记自动释放池吗?人力资源管理:|好的,你有多少数据(大约)?压缩后 Realm 文件的大小是多少?你确定它压缩了吗?
    • 另外,另一个问题是你有多少线程打开了 Realm,如果你是分批插入,或者你为每个项目启动一个新的realm.write()(这很糟糕)跨度>
    • 我正在使用多线程承诺和 PromiseKit 框架。也许,这是错误?
    • 你的意思是github.com/mxcl/PromiseKit?您可能还需要 autoreleasepool 用于您在 Promise 中打开的领域。
    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 2016-06-03
    • 2010-12-30
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2023-03-06
    • 2017-04-11
    相关资源
    最近更新 更多