13

I would like to know how to "clean" memory on Mac OS X. If you hav the developer tools installed, you can use the purge command to clean the memory. However, if the dev tools are not installed, this command will not work. Other applications are able to do this so I know it's possible.

How might one program an app that would "clean" the memory, meaning remove inactive memory and make more space available in free memory?

Yep
  • 653
  • 5
  • 20
  • Good question. I've always wondered how these apps did it. I guess they can't be using private APIs or they would be kicked from the Mac App Store. – sudo rm -rf Jul 20 '11 at 01:21
  • 12
    They don't, really. They just eat as much memory as they can to force other apps to purge. – Jonathan Grynspan Jul 20 '11 at 02:31
  • My friends told me this looks like it might be closed for "off topic". This does relate to programming :/ – Yep Jul 20 '11 at 02:56
  • @Yep I think if you edit the question and make it about the concept of "'cleaning memory' on OS X" or "'cleaning memory' in objective-C" it would be less likely to be closed again should it get reopened (and it would help your chances of it being reopened/answered – Daniel DiPaolo Jul 20 '11 at 14:17
  • Ahh, edit wars. Sorry for overriding someone's edit. :/ – Yep Jul 20 '11 at 14:28
  • @Yep yours is better anyway, this is much better – Daniel DiPaolo Jul 20 '11 at 14:31
  • Actually, the "off topic" close reason has been recently split; it used to cover questions like this. The more appropriate close reason would be "Not Constructive", which means the question... **is not a good fit to our Q&A format.** We expect answers to generally involve facts, references, or specific expertise; this question will likely solicit opinion, debate, arguments, polling, or **extended discussion**.... IMHO, its borderline. –  Jul 20 '11 at 14:42
  • 1
    This question might have some relevant information for this: [clear buffer cache on Mac OS X](http://stackoverflow.com/questions/459537/clear-buffer-cache-on-mac-os-x) – Brad Larson Jul 20 '11 at 17:39
  • Found more info in a comment (by guns) on TUAW here: http://www.tuaw.com/2008/02/21/ifreemem-2-0-when-you-just-dont-feel-like-rebooting/2 – sudo rm -rf Aug 08 '11 at 03:30

1 Answers1

5

One workaround is to malloc as much memory as you can, then free that memory. See the code below taken from another example:

ints = size / sizeof(unsigned int);

unsigned int* mem;
mem = (unsigned int*) malloc(size);

for (i=0; i<ints; i++) {
    mem[i] = rand();
}

free(mem);

Of course it's up to you to decide what you mean by clearing memory, and how much to malloc.

Oscar Del Ben
  • 4,485
  • 1
  • 27
  • 41