Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode shows memory leak, but Instruments does not

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"

like image 362
Mate Hegedus Avatar asked Oct 12 '15 18:10

Mate Hegedus


People also ask

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…”):

What steps do you take to identify and resolve a memory leak in IOS?

The Xcode memory graph debugger helps find and fix retain cycles and leaked memory. When activated, it pauses app execution, and displays objects currently on the heap, along with their relationships and what references are keeping them alive.

How do we avoid memory leaks in closures?

Only capture variables as unowned when you can be sure they will be in memory whenever the closure is run, not just because you don't want to work with an optional self . This will help you prevent memory leaks in Swift closures, leading to better app performance.

How do you detect memory leaks in performance testing?

The best approach to checking for the existence of a memory leak in your application is by looking at your RAM usage and investigating the total amount of memory been used versus the total amount available. Evidently, it is advisable to obtain snapshots of your memory's heap dump while in a production environment.


1 Answers

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.

like image 174
Anders Cedronius Avatar answered Nov 10 '22 07:11

Anders Cedronius