Is there any way to find out how much memory is available in iOS? I know that the system will pass low memory warnings when available memory gets low. However, my App has some points where a single thread will perform a complex task and sometimes that task uses up enough memory that it is just terminated by the OS (my app can download pictures from the internet, and I scale them down to a small size … if the user downloads a very large image, my app runs out of memory and just goes ‘poof’).
Having the App spontaneously terminate is obviously a poor user experience.
Is there any way that I can find out when I am about to run out of memory and stop the task instead?
I suppose I could put the task on a separate thread, and maybe the system would send the main thread a low memory warning, but that seems pretty complicated and not even guaranteed to work.
Thanks!
Ron
While testing and debugging your app with XCode you can use this
logMemUsage()function to NSLog the used/free space and watch how things are going while you test your app. This function logs any change in usage > 100kb. It outputs to the debug log like this (on the simulator the free space is huge):You decide where to call
logMemUsagein your app. I happen to have a function that is called by a timer every second and so I put it in there. I suggest using#ifdefaround these so this code is only included in Debug builds.