Crash Reports
I’ve received a few crash reports for Prison Pushups, many of which looked like out of memory situations. At first, I started looking at the individual reports and problems. As I went it seemed like I was missing the root of the problem and needed to dig deeper. I started using the Allocations tool in Instruments to monitor the memory usage in Prison Pushups. Despite having tested earlier I was worried there could be memory leaks or abandonment causing the app to run out of memory.
Allocations
Now using the allocations was concrete bugs in mind very enlightening. Using the “mark heap” functionality I did find and fix a few small memory leaks that were caused by circular references. It was good to fix these bugs but while looking over the heap I found a much bigger issue…
Cached Images
I found over 50 MB of memory was used after one play through the game! This wasn’t a leak and didn’t grow after more games but it was still a lot of memory. On older devices this would be significant. After a little digging I realized that this was a problem of images being cached by the system when I used
+ [UIImage imageNamed:]
to load the card images for each set of pushups. I did a little research and found that this method is no longer recommend. I also thought about my own usage cases and realized that caching was useless here. I use each card only once per game. There’s never an opportunity to use the cached copy. I switched to using the non-caching method:
+ [UIImage imageWithContentsOfFile:]
This change took a little work as the new method requires the full path to the file while imageNamed: searches for the file one its own. However, the change was worth it, the memory usage dropped from over 50 MB to around 3MB!
Lessons Learned
In the future I’m going to be more aware of my memory usage. I’d run the Allocations tool in Instruments many times on Prison Pushups before release but was concentrated only on looking for leaks. In the future, I’ll question my memory usage and will look over my allocations to make sure they make sense.