13
votes

When I run my app from Xcode, it is clear that I have a memory leak:

enter image description here

When I present a custom ViewController the memory increases, but when I dismiss it it does not go back down.

So I also checked using Instruments' Allocation tool, but that tells a different story:

enter image description here

As you can see, Instruments shows spikes when I present that ViewController, but memory usage drops back to previous levels when it has been dismissed.

I have checked my code at least 15 times and I personally cannot find any memory leaks and thus agree with Instruments. I also implemented the deinit function in all my objects that would get allocated like so:

deinit {
  print("Deinnited \(id)")
}

And all objects printed out to the console as expected.

What could be going on? I am using:

  • iOS 9.0
  • XCode Version 7.0 (7A220)
  • Swift 2.1

Both the schemas for Run and Profile is in "debug"

3
Just checking, but how do you dismiss the views? Also is there idle time? I have found that instruments tends to only show the software side. It will not show you the ram that simply isn't cleaned up yet. When there is no idle time, the OS can't clean up ram. Do you have a singleton? or shared instance for cashing? Probably a strong reference cycle. Try setting stuff to weak. Just throwing ideas around... - R Menke
If you repeat open-close multiple times do you see a rising memory consumption in Xcode? - Darko
@Darko yes. I see a rising memory consumption in Xcode but not in instruments - Mate Hegedus
I mean is it constantly rising and rising until you experience a crash? For this you have to debug directly on the device. - Darko
@Menke I use the dismissViewController on the parent view controller. I use weak references on all delegates. I do not have a singleton. I have checked my code multiple times and could not find any strong reference cycles. - Mate Hegedus

3 Answers

7
votes

It's a extremely annoying Xcode bug or maybe a feature :) if you see it from Apples perspective.

I did encounter it today and here is how to solve it.

  1. Press the application name where you set the scheme (next to the stop button in xcode)

  2. Select 'edit scheme'

  3. Select 'run' (left side of the pop-up window)

  4. Select 'options' (top part of the pop-up window)

  5. uncheck the 'enable backtrace recording' (last selection you need to scroll down)

The downside is that backtrace will be disabled if you crash. So I guess you want to toggle that switch depending on what you want to focus on.

In the 'profile' scheme the backtrace is not enabled therefore you did only see the memory leak when using the debug navigator (run scheme) and not in instruments (profile scheme).

/Anders.

3
votes

After much research on Google and other forums, this seems to be a bug with XCode.

2
votes

Updating previous text after more tool usage experience.

enter image description here

After some recent experiments using the Instruments in Xcode 7.2.1, I noticed that the Leaks chart does not always work. After killing the process of the Instruments and the simulator, run again ... the chart appeared. There seems to be intermittent problem and not found the cause yet.

enter image description here