Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode debug memory graph showing released object

I've been looking in the Xcode Memory Graph tool for a project and was noticing some odd behaviour. Hoping someone might be able to explain what's going on/if I need to worry.

I have a view controller that creates a couple of objects (which aren't doing much at the moment) When I dismiss the view controller sure enough they get released. But one (sometimes both) seem to hang around in the visual debugger:

enter image description here

Only two are ever being created, and both are being deinitialized, confirmed in my consol:

enter image description here

I think I'm correct in saying, I'm not responsible for any of those objects referencing that User object in the image above, so is this a bug, or just something I don't need to worry about?

like image 577
Kyle Goslan Avatar asked Jul 25 '17 13:07

Kyle Goslan


People also ask

How do you debug a memory graph?

Inspect the Debug Memory Graph You can generate a memory graph of the objects and allocations in your app by clicking the Debug Memory Graph button in Xcode's debug area at the bottom of the workspace window. The memory graph shows the memory regions your app is using and the size of each region.

How do I debug memory leak in Xcode?

Diagnose the Memory LeakChoose “Xcode” in the top left of the screen. Expand “Open Developer Tool,” and select “Instruments” Now choose “Leaks,” and make sure you have chosen your target app and device at the top (“Choose a profiling template for…”):

How do I find a memory leak in Swift?

Xcode's Memory Graph Debugger If you haven't used this yet, it's easy to access while developing. Tapping on the icon will pause your application and generate a graph of the objects with their references to other objects. If there's leaked memory detected, you will see purple icons on the left pane of Xcode.


2 Answers

I'm not sure if this is what is happening to you, but this has happened to me and it turns out to be a consequence of Swift's internal memory model, specifically how it handles weak and unowned references.

In order for this to apply to you, something would have to have a weak or unowned reference to your User object.

If this is the case, when the last strong reference to your object is removed, the object will be deinitialized - anything it references will have its reference count decremented and be deallocated if appropriate, and its deinit() method will be run (as shown by your logs). However, it will not be deallocated.

Yes, an object can be deinitalized but not deallocated! The reason for this is that the weak or unowned reference that point at this object must not be allowed to point at deallocated (or worse, re-allocated) memory, because following this pointer would produce "unsafe" behavior. As a result, this section of memory cannot be given up and thus the object is not deallocated.

Two pieces of good news, however. First, only that specific object is being leaked - not any of the objects it references. This means only a tiny bit of memory is being leaked. So it may not be a big deal. Second, for weak references, this behavior is changed in Swift 4 (regrettably unowned references will still have this behavior). It would be interesting to try out your project in Xcode9 beta and see if it still happens.

This is all from asking the same type of question at WWDC. Hope it turns out to applicable!

like image 180
brindyblitz Avatar answered Sep 22 '22 05:09

brindyblitz


If you are not responsible for any of the objects in the graph described, which are the ones retaining the User object, you should not worry about it.

FYI: I have experienced leaks before when including Firebase on an app. It is not your fault, and it should not be leaking a lot of memory.

I agree with @CharlesSrstka , if you are still worried about leaks you can check them too on instruments where it will show you the specific line of code that might be causing the issue.

like image 31
Marcio Romero Patrnogic Avatar answered Sep 24 '22 05:09

Marcio Romero Patrnogic