realm-swift
realm-swift copied to clipboard
Massive memory leak while using LinkingObjects in Swift 3.0.1
We started using LinkingObjects to represent relations between models while Swift was in version 2.2. After upgrading code to Swift 3.0.1 version with Realm 2.0.4 we managed to observe a massive memory leak during data parsing which, after profiling an app, was caused by LinkingObject contruction (over 300MB).
Any chance to release leaked memory?
Realm version: 2.0.4
Xcode version: 8.1
iOS/OSX version: 9.0 - 10.1.x
Dependency manager + version: Cocoapods 1.2.0.beta.1
Hi @LesPatrick, could this possibly be a leak in the Swift runtime, as described in #2933?
In any case, could you please share more relevant information to help us understand what you're talking about and investigate further? It would be helpful if you could share all, or at least some, of the following:
- A sample Xcode project and steps we can take to reproduce and identify the leak.
- If you can't provide 1, an IPA archive of the app causing the leak and steps we can take to reproduce and identify the leak.
- Exported trace data from
Instruments.appso that we can look more into where/why the leak is happening.
I recently did an instruments profile of our app to debug a different issue, and ended up seeing a fair amount of leaks from one of the LinkingObjects we recently added to our app. Basically, every time a Realm model that has a LinkingObjects pointer set up for it (like below), it causes a memory leak.
Here's an example of how we're using it:
class Thing: Object {
private let parentBoxes = LinkingObjects(fromType: Box.self, property: "things")
}
So, in the above, every time a Thing is initialized, it tries to initialize the LinkedObjects object, which triggers [NSObject allocWithZone].
The actual amount of memory doesn't seem to be that much (in my case it was 5KB over a profiled period of a couple minutes), but just thought I'd post this if it's helpful at all.
Other info:
- XCode 8.1
- Swift 3.0
- Realm 2.0.0
I was having a separate issue with the latest release and my investigation showed memory leaks coming from Realm, specifically the LinkingObjects.
I build a dedicated test app: Clonable and downloadable here to isolate the leaky parts of Realm.
Read my comments in the 'ExperimentsOnFriends' class in the test app to see my two tests.
FINDINGS:
Basically, the 'Leaks' profiling tool in instruments will flag LinkingObjects and List<> objects as memory leaks. However, my conclusion is that is an error in the instruments tool as memory does not keep increasing as I move to and from the ViewController that makes and displays my realm objects in the test app.
Some more observations:
LinkingObjectsonly look like leaks if they are unmanaged. My two tests show the difference.Lists<>are always flagged as leaks- Realm must be creating some other stuff the first time it is accessed/used because the memory footprint never returns to the initial level
- These leaks appear in instruments all the way back to 2.0.2. My pod file on the projects lets you switch versions. Just run good old pod update when you switch.
Any observations/comments/scrutiny on my test app is appreciated!
App Run using
xCode: 8.1 phone: iPhone 6 Swift: 3.0
Edit: I included one of my .trace files from instruments in the Git Repo.
Thanks for taking the time to make a sample project for this @joninsky!
Looks like this might be incorrect memory management in RLMInitializeSwiftAccessorGenerics(), which would explain why LinkingObjects and List are affected. I spent a few minutes trying to pin down exactly what's causing the leak and couldn't get further than that.
Please keep discussion on-topic. If you are experiencing difficult with Realm (specifically, not with Swift in general), please feel free to file a new issue.
I found the strange behavior. In simulator (iOS 10.2), there are few leaks. But in real device (iPhone6 Plus iOS 10.2), there are many leaks. We are getting many crash report by Crashlytics that says cannot alloc memory in Realm. I think this issue only exists in real device. Please profile with real device.