Alex Rivera | Logout

How do OS X applications "clean" memory?

Asked 2011-07-20T01:15:12.143
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?

Edit
Report

1 Answer

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.

answered 2011-07-27T06:32:29.500

Your Answer