views:

55

answers:

3

Hey, I am having some trouble finding out how to free ram on the iPhone using Xcode and the iOS SDK. If anyone could give me a hand doing this that would be great.

Thanks in advanced.

A: 

Generally one implements behaviour to free things like cached data, in the -didReceiveMemoryWarning method. Then, when the OS sends a memory warning to your application, that method will be called.

jer
Thanks but i would like it to be a button when clicked it will free up the user memory (ram)
MKDev
Such buttons are implemented using horrible hacks that simply trigger a memory warning. What's more, I believe Apple is now rejecting apps that include this sort of functionality. I would encourage you to stop trying to do this.
Kevin Ballard
A: 

the simulator has a control to simulate a low memory warning

Justin
A: 

If this is for your own use (not for the App store), and you wish to push other apps out of memory, then use a repeating NSTimer to continue to try and allocate (malloc) large and random page sized memory blocks, say 30 times a second for several seconds, and/or until you can't allocate any more, all the while mostly ignoring memory warnings.

Then free all these allocations at once... if your app is the one left running by the OS.

hotpaw2
If you try to allocate too much memory, your app will get jettisoned.
Kevin Ballard
That is such a horrible thing to do. Don't do that.
bbum
@Kevin Ballard: False. If you try to malloc more memory than is available, all the happens is you get a NULL pointer back. As long as you don't try to access memory using that NULL pointer, your app will not get jettisoned. And you can try again later with a different sized allocation.
hotpaw2
@bbum. If it's for your own personal use, then it is no more horrible than killing all the other apps using the multi-tasking bar. Useful for high-watermark vs. low-watermark app performance testing.
hotpaw2
@hotpaw2: if your allocation is successful but you've still used up too much memory, the system will jettison your app.
Kevin Ballard
Im afraid this is actually what I want. If you could tell me how to do this that would be great. However will my app be rejected if I implement this feature?
MKDev
If the app crashes during review it will be rejected, which can happen (but not always if you're careful) using this technique. You might try freeing a small amount of memory (experiment with the amount) every time you return to the run loop.
hotpaw2