IBM Developer Day | Bengaluru | March 14th Register now
By Taylor Franklin | Published November 7, 2014
Managing memory is important and necessary for anyone developing native iOS applications, and that’s still true if you’re using Apple’s new programming language, Swift. I’ve come across and solved several memory issues here at IBM’s Mobile Innovation Lab.
Objective-C has never implemented garbage collection in the way languages like Java have. When developers first started using Objective-C, they had to manage their memory manually under a Reference Counting system. For the most part, this just meant adding the keywords retain, release, and autorelease when allocating and deallocating objects.
A few years ago, Apple released Automatic Reference Counting (ARC) in Xcode 4.2, which took the load off developers having to manually do the reference counting. It accomplished this simply by doing everything a developer would do to manage memory. Even with ARC, though, memory issues can still happen, so we’ll look at traps to avoid using ARC and techniques for debugging and finding memory issues in your application.
Go to the Apple developer site to learn more about the old ways.
In the most recent Objective-C app I worked on, fixing a retain cycle showed the biggest improvement in memory of all the memory bugs I fixed. A retain cycle is essentially when two objects retain each other. This goes against the standard rules of object ownership, leaving both objects with no authority to release the other, causing a memory leak (the numbers are the retain count):
As the diagram above shows, two objects have a reference to object B, so that when object A is released, object B lives on because object C is referencing it and now there is nothing left to release either object B or C. Thus, we see ARC removed our concerns with retain/release calls, but we still need to think about strong and weak pointers as well as retain cycles. Retain cycles are also possible in Swift , since it also runs on ARC.
Read Swift documentation about ARC (examples of cycles).
The app I was working on did a lot of image handling. My team became aware of memory issues when, on an iPhone 4S, the app would crash after about 5 minutes of running our UI automated tests. I eventually came across something like the following:
@property (nonatomic) id<MyDelegate> delegate;
In one of my viewcontrollers, we were assigning self to this delegate, which at first glance seems fine. Then I found out properties that don’t specify are implicitly strong. So this property was holding a strong reference to my viewcontroller, which prevented it from ever being deallocated, causing memory to climb fairly rapidly. Once I figured this out, I promptly made this property weak:
@property (weak) id<MyDelegate> delegate;
After adding the weak attribute to that property, the app ran for 45 minutes on an iPhone 4S — nine times better by changing just one line of code!
In our app, we were downloading a lot of different images and caching them, which is not the ideal use case for caching. Caching is ideal for storing frequently accessed objects, and we were not frequently accessing these images. Although, NSCache and other caching libraries should discard parts of the cache when you are running low on memory, some developers may want more control.
Issues in caching can also be present with Swift as with Objective-C, because many Apple frameworks (Foundation, UIKit, CoreLocation) are still written in Objective-C. Nevertheless, you as a developer need to decide if it would be beneficial to use a caching system; otherwise, it can cause unnecessary memory growth over time.
I wanted greater control of the cache for the app we were working on, so I used a caching library that cached the photos for our table view until that view was deallocated; this is when we cleared the cache. This way, our table view scrolling was smooth, but we did not hold images in memory more than necessary.
In our Objective-C app, not knowing what ARC handles was not much of an issue, but I did make sure that we were handling C code appropriately. ARC essentially does not apply to C iOS libraries (such as Core Graphics, Core Text) or self-written C code. In Core Graphics, you have to release variables yourself with C function calls:
CGImageRef imageRef = CGImageCreateWithImageInRect([self.cropView.imageCGImage], CropRect);
cropped = [UIImage imageWithCGImage:imageRef];
We used that piece of code to do the actual cropping in our app, but notice we created a Core Graphics image reference and then promptly released it after we were done.
Note for Swift: This case does not directly correlate to how things work in Swift. For Core Graphics specifically, developers do not have to manually release CGImageRefs; ARC actually does handle this for you in Swift. This is not true for all C libraries in Swift, however, so be sure to check the documentation before you make any assumptions. The fact is, Swift has nice new features and a lot of syntactic changes, but it still has some pretty close ties with Objective-C.
Overriding the dealloc method on a viewcontroller will help ensure that a viewcontroller is being deallocated when you expect it to be. It can be as simple as the following Objective-C example:
NSLog(@"viewcontroller is being deallocated");
Using this is technique helped me find the retain cycle in our app. It showed that a certain viewcontroller was never being deallocated, but more instances of it were being allocated, thus creating the big memory leak. This is one of the first strategies I would follow in debugging memory issues.
The Apple instructions for creating autoreleasepools say that developers typically will not need to do it themselves. There are some cases, however, where creating your own can help reduce the peak memory footprint of your app. Autoreleasepools help because instead of waiting for the system to release whatever objects you have made, you are telling the system to release those objects at the end of the block (or closure in Swift). This example will help explain:
for (int i=0; i<5000; i++)
NSNumber *num = [NSNumber numberWithInt:i];
Memory probably doesn’t get too high when we are just dealing with numbers, but the objects are being released as you iterate through the loop and not all at the end. When dealing with objects like UIImages or even base64 NSStrings, memory could climb pretty high before any of those objects are released. I used a few autoreleasepools in our app, but since we were not dealing with a lot of memory heavy data at any one time, I don’t think it made a huge difference.
Note for Swift: Few things have changed with memory management in Swift, but using autoreleasepools actually has. When working in a Swift project that also uses Objective-C, there’s a chance you might need to use or could use autoreleasepools. If you are working in a purely Swift project, then you should never have to write an autoreleasepool closure. By the way, the syntax is the same for autoreleasepools in Swift, but without the @ symbol at the beginning.
Watch Improving Your App with Instruments (from the WWDC 2014 conference).
If you have been debugging your code and are not sure what is causing your memory problem, it can be helpful to isolate pieces of code you are suspicious of. For example, you can temporarily set up your code to call a particular method many times and see how your memory allocations and deallocations react. Also, in a navigation-based app, it’s helpful to continually push and pop the viewcontroller with a potential problem method and see if everything you expect is being released on each pop.
At one point in our app, we weren’t sure if an image-cropping view was causing our app’s memory to climb, so I isolated the whole viewcontroller inside a new Xcode project. After analyzing the project in Xcode Instruments, I saw nothing that would lead me to believe this viewcontroller was the issue. I performed this practice throughout my debugging, and it was effective in showing where the memory problem was not, saving me a lot of time in meticulous debugging.
My last tip for you is using Xcode Instruments for debugging. Instruments an application in the Xcode toolset. It has everything you might need to debug, test, or optimize your app. Within the Instruments app, there are many kinds of instruments such as Allocations, Leaks, Automation, and Time Profiler, to name a few. I’ll focus on the Allocations instrument in the Xcode 6 Instruments tool. I used it the most when debugging our memory issue, and I’m sure it can help you now or in the future.
Before we start, you can open Instruments from a spotlight search or straight from Xcode, like so:
By clicking the plus button in the top right, you can add more instruments for different kinds of testing, but I won’t be covering them in this tutorial.
Before running any analysis, there are a few things you need to do. First, you need to plug in an iOS device that has your app installed on it. It must be a physical device because the iOS Simulator is still a simulator and may not accurately represent memory use in your app or how an app might perform under memory pressure.
To pick your target, click My Computer near the top, hover over your device, and then pick your app from the sub-menu:
Next, there is a panel where you can alter the settings for the types of allocations you will be viewing. Besides making sure the Created & Persistent bubble is checked, there is not much you need to do beforehand.
Optional: I like to zoom in on the allocations view. You can do this by clicking Allocations on the left and pressing Command+.
Once you press the Record button in the top left, your app will start up on your device, and Instruments will begin to chart your allocations. All you need to do here is run through your app, focusing on possible problem areas to see if more memory allocates than deallocates. This could mean doing a lot of repetitive tasks, but you’ll thank yourself later.
You should see something like this:
I recommend running through your app once and getting to a stable point in memory so you have a good baseline that will make any increase noticeable. When you are satisfied you have enough data to test, press the stop button in the top left.
There are different ways to analyze this data that I won’t cover here, but be aware that there’s a whole drop-down menu of ways to view and analyze your data.
If you prefer not to deal with the inspection ranges as much, there is a feature called Mark Generation. There is a button for it on the right panel of instruments.
This button will mark points on the timeline of instruments based on where the inspection line is. It does this in order to keep track of all the allocations since the previous mark, or from the beginning if there are no other marks. You can mark generations as you are running the allocations instrument or after you have stopped the run, as in this example:
I did another run, and as you can see, little red flags appeared on the timeline as I marked each generation. I can also see the generation view on the bottom and view all the data and how it varies from generation to generation. This is a quick way to see the growth between screens as I navigate through our app. Depending on your situation, this can be very useful, but other times it may just be easier to manually set your inspection ranges after you’ve recorded your session.
The last thing to cover is looking at the stack trace. For this, you want to set your inspection range to highlight all the allocations, and then look at the statistics view, making sure the Created & Persistent bubble is selected on the right panel. In the statistics view, make sure Persistent Bytes is sorted from highest to lowest. There are a lot of allocations here, and it can be hard to understand what is going on, since a lot of them are system allocations.
Look at the largest allocations and click on the right-facing arrow. A lot of times there will be allocations inside the ones you clicked on and many of them won’t have meaning to you.
As you highlight different allocations after clicking an arrow, continue looking at the extended detail on the right panel. Eventually you will come across some bold text that leads to actual code in your project, telling you what the issue might be.
If you double-click one of the bold items in the stack trace, it will take you to the actual code (assuming you ran allocations on an app you own).
There are a lot of useful things about this view, one being the mostly yellow tags on the right showing you just how much memory each method call is taking up. Every app is different so you, the developer, have to decide if the highlighted method is a problem, something you can optimize, or just an unavoidable part of your app.
Things are always changing, with Xcode updates and new developments like Swift. As long as iOS development does not implement a garbage collector, you need to be careful to avoid memory leaks — preferably during the development process and not when your application is almost finished. I hope the tips in this tutorial will help you fix many of your memory issues and build memory-efficient apps.
February 4, 2019
Mobile DevelopmentObject Storage+
March 6, 2019
We’re pleased to share the first release of a new mobile application that surfaces IBM Developer content in a convenient,…
Back to top