views:

1325

answers:

3

How to display iPhone free memory in a UILabel ?

and I would like to ask, how to free up memory using iPhone SDK?

FYI, I'm using iPhone SDK 3.1.2 with xcode 3.2.1 (Mac OS X Snow Leopard).

A: 

You can free up memory by calling "release" on objects you've allocated after you're done with them. See http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

kprevas
Is there any way to free up the memory of whole iPhone system, instead of a class itself? I notice that an app called SBSettings can do the job, but I don't know how they achieve it.
Shivan Raptor
+4  A: 

There is no direct access to this information in the SDK, and apps are not allowed to mess around with the OS the way you're suggesting. You're in your own little box; Apple expects you to stay there. When you ask questions that include "the system," you are probably outside of your box.

That said, there are some tricks you can use. You can allocate increasingly-large blocks of memory until you receive -didReceiveMemoryWarning. At that point, release your allocated memory. That will give you a rough idea of how much memory the OS will allow you to have. That's not the same as how much the OS has of course. Tripping -didReceiveMemoryWarning will also cause the OS to do some housekeeping of its own, so this is pretty much the most effective programmatic way you have to free up memory.

The most effective non-programmatic way to free up memory is to ask the user to reboot. For applications that require substantial memory (generally games), this approach is not unheard of.

SBSettings requires jailbreaking and is outside of the SDK.

Rob Napier
Thank you for your answer. I would like to ask a bit further, at which level, iPhone will see the memory is low? say 10% memory remaining?
Shivan Raptor
It is not documented and is difficult to determine because the OS's own use of memory is always fluctuating. It is completely possible for you to receive a memory warning, do nothing, and everything be fine because the OS freed memory on its own. Due to memory fragmentation, it's also possible to allocate more memory using small blocks than large ones, so the issues can be complicated.
Rob Napier
A: 

It is possible to determine the amount of free memory on an iPhone using the mach api. See this related question: http://stackoverflow.com/questions/2798638/available-memory-for-iphone-os-app

Josh Knauer