When I run my app from Xcode, it is clear that I have a memory leak:
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:
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:
Both the schemas for Run and Profile is in "debug"
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…”):
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.
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.
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.
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.
Press the application name where you set the scheme (next to the stop button in xcode)
Select 'edit scheme'
Select 'run' (left side of the pop-up window)
Select 'options' (top part of the pop-up window)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With