One reason that developing software for mobile devices can be so much more challenging than developing for desktops/laptops or other hardware, according to ThoughtBot’s Steff Kelsey, is that resources are limited. One particularly sparse resource is RAM. Because of this, Out-of-Memory exception errors (OOMs) are common on iOS, especially if you’re dealing with large files in your app.
Because they are so common and important to overcome, we’ve mentioned Out-of-Memory exceptions and their causes before. There are many factors can cause apps to terminate early in this way, including in-app aspects like CPU pegging, app memory usage, and broken screens. Out-of-app elements like the device’s OS, hardware, or memory being devoured by other apps can also play a role in app termination. What’s more, the complexity of how these multiple factors relate to one another causing an OOM can make the problem a difficult one to solve.
Does the Device You’re Developing for Increase the Chances for OOM Errors?
If we revisit the first paragraph of this story, we can see that a primary issue related to OOMs is RAM usage and it’s limitations on mobile devices. At the risk of stating the obvious, larger devices have larger screens requiring higher resolution images for viewing, thus taking up more memory. This extra imagery baggage can cause memory leaks and inevitable RAM-draining exceptions.
What Can Be Done to Eliminate OOM Errors on Larger Devices?
Broadly speaking, reducing an app’s total memory usage is a crucial first step to minimizing OOMs. Therefore, when you are developing for larger format mobile devices, you may have to be a bit more thorough in making sure coding approaches are efficient. This extra attention will ensure loops or calls that might be benign with smaller images won’t go rogue when referencing images with more heft. Other common causes of OOM errors—and solutions for preventing them—include:
- Improper use of UIImage. This can result in too many images being held in memory at the same time. Therefore, care taken in destroying images hogging resources can help eliminate OOM errors.
- Retain cycles in View Controllers. This happens when one object strongly references a view controller in such a way that it can cause a loop. Breaking excessive loops is another fix.
- Setting the in-memory cache to scale with the size of the images it’s initialized to contain—whether using FastImageCache or otherwise—can prevent the device resources and RAM from being used up.
There are always other scenarios, but these are a few common solutions.
Will This Be Enough?
One thing that can be troubling is that many of these problems only become visible after your app has been released into production. As developers, we try to deploy on a number of test devices, but even the best QA approaches can be limited in the number of devices tested. Ultimately, an app can get to end user devices that may not have been profiled. Experienced development processes and code reviews can prevent these issues, but additional tools are always helpful.
Insights and device profiling assistance from a tool like Embrace.io can help get ahead of potential bottlenecks and lessen the need for pushing out short turnaround technical hotfixes.