1

one of my app needs the function that free inactive/used/wired memory just like command 'purge'. Check and google a lot, but can not get any hit

Welcome any comment

arachide
  • 8,006
  • 18
  • 71
  • 134

2 Answers2

2

Purge doesn't do what you seem to think it does. It doesn't "free inactive/used/wired memory". As the manpage says:

It does not affect anonymous memory that has been allocated through malloc, vm_allocate, etc.

All it does is purge the disk cache. This is only useful if you're running performance tests and want to simulate the effects of "first run after cold boot" without actually cold booting. Again, from the manpage:

Purge can be used to approximate initial boot conditions with a cold disk buffer cache for performance analysis.

There is no public API for this, although a quick scan of the symbols shows that it seems to call a function CPOSXPurgeAllDiskBuffers from the CoreProfile private framework. I believe the underlying kernel and userland disk cache code is all or mostly available on http://www.opensource.apple.com, so you could do probably implement the same thing yourself, if you really want.

As iMysak says, you can just exec (or NSTask, etc.) the tool if you want to.

As a side note, it you could free used/wired memory, presumably that memory is used by something—even if you don't have pointers into it in your own data structures, malloc probably does. Are you trying to segfault your code?

Freeing inactive memory is a different story. Just freeing something up to malloc doesn't necessarily make malloc return it to the OS. And there's no way you can force it to. If you think about the way traditional UNIX works, it makes sense: When you ask it to allocate more memory, it uses sbrk to expand your data segment; if you free up memory at the top, it can sbrk back down, but if you free up memory in the middle, there's no way it can do that. Of course modern UNIX systems don't work that way, but the POSIX and C APIs are all designed to be compatible with systems that do. So, if you want to make sure memory gets freed, you have to handle memory allocation directly.

The simplest and most portable way to do this is to create and mmap a temporary backing file, or just MAP_ANON, and explicitly unmap pages when you're done with them. (This works on all POSIX systems—and, with a pretty simple wrapper, even Windows.) If you need even more control (e.g., to manually handle flushing pages to disk, etc.), you can use the mach/mach_vm.h APIs.

abarnert
  • 354,177
  • 51
  • 601
  • 671
0

You can directly run it from OS // with exec() function

iMysak
  • 2,170
  • 1
  • 22
  • 35
  • use exec() system looks like freezing – arachide Jul 11 '12 at 10:46
  • I'm not sure what that comment means, but maybe you don't realize what exec does. (I'm assuming here you're writing in C, ObjC, or C++, but you didn't actually say.) As "man 3 exec" will tell you, it replaces your current process with the one you're trying to run. So, unless you actually want to quit and "hand off" to purge, the way to use it is to fork(2), then exec(3) in the child process. Or, more simply, use the posix_spawn(2) wrapper. Or, if you're more comfortable with the Cocoa level, use NSTask. – abarnert Jul 11 '12 at 19:12