Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responding to RAM availability in iOS

I have a texture-heavy OpenGL game that I'd like to tune based on how much RAM the device has. The highest resolution textures I have work fine on an iPhone 4 or iPad2, but earlier devices crash in the middle of loading the textures. I have low-res versions of these textures, but I need to know when to use them.

My current tactic is to detect specific older devices (3GS has a low-res screen; iPad has no camera), then only load the hi-res textures for IPad2 and above and iPhone 4 and above — and I suppose I'll need to do something for the iPod touch. But I'd much rather use feature detection than hard-coding device models, since model detection is fragile against future changes to APIs and hardware.

Another possibility I'm considering is to load the hi-res textures first, then drop and replace them with lo-res the moment I get a low memory warning. However, I'm not sure I'll get the chance to response; I've noticed that the app often dies before any notification appears on the debug console.

How do I detect whether the device I'm running on has insufficient RAM to load hi-res versions of my textures?

Taking a step back, is there some other adaptive technique I can use that's specific to OpenGL texture memory?

Notes:

  1. I've searched on and off SO for answers related to available RAM detection, but they all basically advise profiling memory usage and eliminating waste (minimising the lifetime of temporaries, and all that guff). I've done as much of that as I can, and there is no way I am going to squeeze the hi-res textures into the older devices.

  2. PVRTC isn't an option. The textures contain data to be used by fragment shaders and must be stored in a lossless format.

like image 926
Marcelo Cantos Avatar asked Nov 07 '11 04:11

Marcelo Cantos


People also ask

How do I check my RAM on iOS?

Question: Q: How to check RAM in iPhoneThere's really no way. Each iPhone model has a set amount of RAM that's the same regardless of the storage capacity. Apple doesn't publish the amount of RAM in their technical specifications, but many sites have that information available.

How much RAM can iOS apps use?

iPadOS 15 Allows Apps to Use Up to 12GB of RAM on High-End iPad Pro, Up From Just 5GB. In June, we reported that starting with iPadOS 15, Apple is giving developers the ability to allocate their apps more RAM, allowing apps to use more of the available memory in the iPad to run faster and smoother.

How is memory management handled in iOS?

Memory management in iOS was initially non-ARC (Automatic Reference Counting), where we have to retain and release the objects. Now, it supports ARC and we don't have to retain and release the objects. Xcode takes care of the job automatically in compile time.


2 Answers

To get the total (maximum) physical RAM of the device use [NSProcessInfo processInfo].physicalMemory.

See Documentation.

like image 149
Steve Avatar answered Oct 10 '22 08:10

Steve


Total physical RAM is available via sysctl(), as documented in this blog post and implemented as a nice clean API here (see the implementation of totalMemory in the corresponding .m file).

I've lifted the blog's code for convenience and posterity:

#include <sys/sysctl.h>

size_t phys_mem()
{
 int mib[] = { CTL_HW, HW_PHYSMEM };
 size_t mem;
 size_t len = sizeof(mem);
 sysctl(mib, 2, &mem, &len, NULL, 0);
 return mem;
}

I don't know if Apple will approve an app that uses sysctl() in this manner. It is documented, but only for Mac OS X.

like image 29
Marcelo Cantos Avatar answered Oct 10 '22 09:10

Marcelo Cantos