Realm-cocoa: Realm config with objectTypes leads to memory leak

Created on 23 Jan 2018  路  8Comments  路  Source: realm/realm-cocoa

Goals


Instantiating a Realm instance with a custom configuration which includes a bunch of custom classes in objectTypes.

Expected Results


No memory leaks :-)

Actual Results


Crazy memory leaks.

Steps to Reproduce


See code snippet below.

Code Sample


Instantiating the Realm instances in bulk:

for _ in 0...100 {
    autoreleasepool {
        _ = try? Realm(configuration: realmConfig)
    }
}

This is the realmConfig that we are using:

realmConfig = Realm.Configuration(
    fileURL: filePath,
    schemaVersion: UInt64(databaseSchemaVersion),
    migrationBlock: migrationBlock(),
    shouldCompactOnLaunch: { totalBytes, usedBytes in
        // totalBytes refers to the size of the file on disk in bytes (data + free space)
        // usedBytes refers to the number of bytes used by data in the file
        // Compact if the file is over 100MB in size and less than 50% 'used'
        let oneHundredMB = 100 * 1024 * 1024
        return (totalBytes > oneHundredMB) && (Double(usedBytes) / Double(totalBytes)) < 0.5
    } ,
    objectTypes: [Type1.self, Type2.self, Type3.self, Type4.self, Type5.self, Type6.self,
                  Type7.self, Type8.self, Type9.self, Type10.self, Type11.self, Type12.self]
)

The autoreleasepool is only used to amplify the memory leak. It still increases without, just a lot slower.

Instantiating Realm with the default config or without the objectTypes doesn't increase the memory.

Version of Realm and Tooling


Realm framework version: 3.1.0

Realm Object Server version: -

Xcode version: 9.2 (9C40b)

iOS/OSX version: Tested on iOS 11.2.2

Dependency manager + version: Carthage 0.27.0

Most helpful comment

Cool, thanks for the quick fix!

Are you guys planning to do a hot fix release for this?

All 8 comments

Looks like it's copying the schema each time the Realm is opened, which on top of being kinda slow results in a new set of accessor classes being created every time rather than them being reused. Should be easy to fix.

Good find @EmDee ! We have some sort of memory leak but were not able to really track it down to this until now. I can confirm removing objectTypes from the config will fix the memory leak.

As we see this problem too, is there a workaround for the meantime? We have two separate frameworks which both use realm and are used together in our app. Removing the object classes from the config has the downside of a mirrored realm schema in both libraries and causing the need for weird migrations (since those two frameworks are not aware of each other).

Cool, thanks for the quick fix!

Are you guys planning to do a hot fix release for this?

馃憤 for that dude

Is it fixed or no? It seems to me no. Objects are leaking

AFAIK this was fixed for us

The leak we faced has been fixed in the above linked issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carvalho-oak picture carvalho-oak  路  3Comments

TheHmmka picture TheHmmka  路  3Comments

jpsim picture jpsim  路  3Comments

javierjulio picture javierjulio  路  3Comments

menhui222 picture menhui222  路  3Comments