Learn best practices for using memory efficiently in iOS and tvOS.
Efficient memory usage is a critical consideration for resource-intensive Metal-based apps in iOS and tvOS. Apps should use as little memory as possible, making more memory available for other apps and system services.
iOS and tvOS monitor your app’s total memory usage at runtime, and if the amount exceeds a predetermined limit, the system terminates your app. This limit varies by device model, so be sure to test your app on all supported devices.
Understand Changes in Memory Accounting
The accounting for purgeable, nonvolatile memory changed beginning in iOS 12 and tvOS 12. In iOS 11 and tvOS 11, allocations with this memory storage mode—commonly used by Metal apps to store buffers, textures, and state objects—weren't counted toward an app’s memory limit and weren't presented in tools like Xcode memory gauge. It was possible for an app’s actual memory usage to exceed certain predetermined memory limits without being terminated.
In iOS 12 and tvOS 12, if an app is built with the iOS 12 or tvOS 12 SDK, such allocations are counted toward the app’s memory usage limit. As a result, the system can now more accurately monitor an app’s total memory usage. If an app exceeds the limit, it's now likely to be terminated on devices running iOS and tvOS versions 12 and later.
As of March 27, 2019, all new apps and app updates for iPhone or iPad, including universal apps, are required to be built with the iOS 12.1 or later SDK and support iPhone XS Max or iPad Pro (12.9-inch, 3rd generation). If you have difficulty reducing your app’s memory requirements, contact Apple Developer Support to request an entitlement for your app to use iOS 11–style memory accounting.
Optimize Memory Usage in Your Metal App
Here are some tips for understanding how your Metal app is using memory, and best practices for reducing memory usage.
Use Xcode 11 to measure memory consumption. Build your app with the iOS 13 or tvOS 13 SDK, then use Xcode memory gauge to observe your app’s total memory footprint during execution.
Use these tools to gain a deeper understanding of your app's memory footprint.
Use the Frame Capture Debugging Tools to take a frame capture. Then use the Metal Memory Viewer to get a visual representation of all of a frame's GPU resources and a table of properties for each resource.
Use the memory graph tool to help identify leaks and abandoned memory.
Query the amount of memory available to your app at runtime using the os_proc_available_memory API to help you identify memory spikes.
Make your assets as small as possible. Avoid loading a large resource when a smaller one will do. Use compressed texture formats. Load lower-resolution textures when running on memory-constrained devices. Consider reducing the fidelity of 3D models and compressing per-vertex data.
Simplify memory-intensive special effects. Some effects, like shadows or motion blur, require large offscreen buffers for temporary image data. Consider lowering the resolution of these image buffers or applying fewer effects when running on memory-constrained devices.
Avoid loading unused resources. Xcode can help you identify Metal objects that aren't in use. Use the Frame Capture Debugging Tools to take a frame capture. Then navigate to the Metal Memory Viewer, and select the Unused filter.
Designate resources as volatile when possible. Use
set for textures and buffers whose underlying memory could be discarded under low-memory conditions, and subsequently recreated or reloaded as needed. This allows your app to keep a cache of idle resources in memory while avoiding counting them toward the memory limit.
Use memoryless texture storage for temporary render targets.
MTLStorage avoids allocating regular system memory, allowing apps to store the contents of temporary render targets directly in tile memory on the GPU.
Use Metal resource heaps.
MTLHeap allows multiple Metal resources to be backed by the same memory allocation. For example, you can use resource heaps when transient resources are produced and consumed for each frame but aren't all used together. Those not used together can share the same memory, backed by a heap.
The WWDC video Delivering Optimized Metal Apps and Games provides additional information about the latest memory debugging tools and best practices to help you optimize the memory footprint of your Metal apps and games.