Realm-cocoa: Instantiating first Realm causes memory leak originating in Swift reflection code

Created on 3 Dec 2015  路  20Comments  路  Source: realm/realm-cocoa

I've started trying to fix memory leaks on an app, and noticed that there seems to be a leak when instantiating a Realm.

Here's example project where I've included a few screenshots and the Instruments trace showing the problem.

https://github.com/jviney/realm-memory-leak-test

Screenshot 1 Screenshot 2 Screenshot 3

The number of objects leaked correlates with the number of properties your Realm objects have. If you comment out the definition of the Person class the leak is smaller.

Thanks in advance.

T-Bug swift

Most helpful comment

Whatever the cause, this leak still seems to be happening with Realm 0.103.1 and Xcode 7.3.1:

image

All 20 comments

I also have this leak on my app, cause crash on some users' devices.
Any solution to fix?
Thanks a lot

Thanks for the reproducible test case, @jviney!

The leaked _NSContiguousString instances never appear to escape from the Swift runtime, which makes that leak feel like a bug within the Swift runtime.

The leaked _NativeDictionaryStorageOwner relates to the listProperties member of Realm's DynamicObject class, but no DynamicObject instances are leaked. Most of the retain / release traffic on the leaked instance, including the mismatched retain, is within Swift's reflection machinery.

@vtongcoder These leaks happen during schema initialization, which is typically a one-time event during application initialization, and therefore shouldn't significantly impact the memory usage of your application. It's hard to see how this leak could relate to any crash you're seeing. It'd be great if you could file a separate GitHub issue about your problem.

Small world @bdash ! You're working at Realm now :+1: ! Thanks for looking into this issue.

Agreed that these leaks are pretty small and shouldn't significantly increase an app's memory usage.

I agree that it's not such a big leak, but anyways, it would be great to have 0 leaks in the app, so I'm reporting that the leaks during initialization are still a case (as of 0.98 Realm Swift):

Screenshot 1
Screenshot 2

YES! It's not just me! All of these leaks in my screenshot are from my Realm init.

screen shot 2016-02-09 at 1 41 41 pm

We are seeing the same thing on the current version of SwiftRealm and would love to get this cleaned up.

Also here, always getting a leak in Realm()

same problem

v0.98.8 still getting a leak when Realm object is initialized

Lots of people have commented here, but has anyone filed a radar or Swift bug? Based on @bdash's earlier comment, this appears to be a leak in the Swift runtime, which Realm Swift happens to trigger:

The leaked _NSContiguousString instances never appear to escape from the Swift runtime, which makes that leak feel like a bug within the Swift runtime.

I've retitled this to make it clear that this is about a one-off leak originating in Swift's reflection machinery that occurs when initializing the shared schema. As best as I can tell, this is due to a bug in the Swift runtime.

We've also had recent reports of a leak related to RealmCoordinator::get_realm which is being tracked in #3270. Though I've not spent any time analyzing this second report, it appears at first glance to be a bug on our end.

I did verify that #3270 is a bug on our end yesterday (the initial refcount is wrong so it never gets freed, but now I need to verify that everything is correct with them actually being destroyed).

Whatever the cause, this leak still seems to be happening with Realm 0.103.1 and Xcode 7.3.1:

image

same memory leak for the _NSContiguousString

I'm getting the same _NSContiguousString leak as well in 1.1.0.

This is my capture:
screen shot 2016-09-28 at 9 18 28 am

Any response from Apple on this @zhenjiangliu24 @mjohnson12 @gabovanlugo? Have any of you filed radars or tickets on https://bugs.swift.org that we can dupe or follow?

Any update for this thread?

Any workaround?

The leak as reported by @jviney (and demonstrated in his test project) appears to be fixed as of Xcode 8.3 with Swift 3.1. I've shared the updated project I tested with at https://github.com/bdash/realm-memory-leak-test.

If anyone is seeing leaks of any kind with Xcode 8.3 and Swift 3.1, it is very likely to be a different issue than was originally reported here. You'll need to file new issues about what you're seeing since the problem this GitHub issue was tracking has been fixed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jpsim picture jpsim  路  3Comments

TheHmmka picture TheHmmka  路  3Comments

i-schuetz picture i-schuetz  路  3Comments

fadylateef picture fadylateef  路  3Comments

duribreux picture duribreux  路  3Comments