Search code examples
iosdebuggingswiftmemory-leaksipad-mini

how to debug memory warnings in Swift?


I have an app I am developing and it definitely is having some memory issues after extended use on the iPad mini 1. The debugger will start spitting out memory warnings until it crashes and I would like to know what controller in our code is causing this problem.

Here is an example of the debug message.

2015-03-11 17:04:02.420 PROJECT_NAME[154:4100] Received memory warning.

The Debug message I get gives a timestamp and the Project Name with some info in brackets. How can I use this to find what controller is causing this problem? The bracket info doesn't seem to be code line numbers since the name before it is the project name not a file name.

The other alternative I've been thinking is possibly putting a print statement in the controllers didRecieveMemoryWarning() function.

What is the best to track down and debug these in Swift?

Let me know if I need to provide more info.

UPDATE

I am also curious to know about some of the common causes for memory leaks in Swift? In my case I am using a UITableView that navigates through a navigation controller to another UITableView. The second table view contains some custom cells which use mainly texts and small images. I'm assuming that the images (although small and mainly for asthetic purposes) are whats causing the issue. I'm going to use instruments as suggested next to debug this to find out exactly what is doing this.

UPDATE

So I've been playing in the profiler trying to find things that could be causing memory leaks, and allot of the things in the profiler seem to be code that is library code, but I did after some sifting find some references to stuff that was in my code. One consistently was repeated being the following.

enter image description here

This code is inside the a custom UITableViewCell awake from Nib method, and has to do with setting the initial state of an image that will change depending on the state of the data that belongs to that cell. I'm guessing that this dynamic image loading could be causing one of these problems. The question is, what do I do to fix it?


Solution

  • At this point, you need to change gears if you want to identify the source of your memory problems. Use Instruments as illustrated in WWDC 2013 video Fixing Memory Issues or WWDC 2012 iOS App Performance: Memory. Using Allocations tool in Instruments you can identify what is being allocated and not being released. Only then can you proceed to resolving the issue (e.g. fixing any strong reference cycles, etc.). We can't advise you how to remedy the problem until you identify the source of the problem, and Instruments will help you do that.

    While those presentations illustrate the practice with Objective-C, the principles are the same in Swift, and the use of the Allocations and Leaks tools in Instruments is the same.