Purge command - Unable to purge disk buffers, error #-2? When I attempt to run the purge command, I get the same error every time.

[ERROR] &lt;CPDevice.c:3816&gt;   Unable to create new counter client.
[ERROR] &lt;CPOSX.c:1172&gt;  Unable to get user client so as to poke the kernel. 
Unable to purge disk buffers, error #-2.


I've tried with and without sudo, initially thinking that perhaps it's unable to due to not having proper access, and they simply didn't program in a permissions check. It's the same error though.

I have a dual Xcode installation, which I believe may be the issue here. I have 3.2.6 installed to a custom directory (/Xcode 3) and I have Xcode 4.6.3 from the Mac App Store. I've installed the most recent (2013.4) Command Line Tools from within Xcode 4. I'm running OS X 10.7.4 Lion.

Before anyone tells me that I should let the OS manage inactive memory on it's own because it does it fine, no, it doesn't. Not in the slightest. Normally it works well enough not to bother me. But whenever I use any type of VM, I encounter issues with the memory never releasing. 

For example, I can start an instance of an emulation VM. It'll use about 600 MB of memory. When I terminate the emulation entirely, the memory does not release. On top of that, if I go back and rerun the exact same emulation, the 600 MB sitting in inactive doesn't get reused like one might hope. No, it just sits there. And another 600 from the free pool gets used. And when that second emulation is terminated, I now have 1.2 GB of inactive memory. Now normally this would be fine, if the inactive memory released when all the free memory was used. But it doesn't. It just sits there and never releases. It'll swap to disk instead. 

This issue is particularly annoying with virtualization VMs that use 4 GB or even more of RAM within my normal usage. Restart those more than once and new things slow to a crawl due to disk swapping.

I used to use the purge command all the time on 10.5 Leopard with Xcode 3.1.4, and issues on Leopard weren't nearly this bad with automatic memory management. I just used purge to get more of a clean slate without having to reboot. I can't even do that now. And unlike some people would tell you, it is a serious problem.

Any ideas would be appreciated. One more thing. I have used xcode-select to select Xcode 4.6.3 as opposed to 3.2.6. I only use 3.2.6 for development and compiling. Macports, Homebrew, and some other things, demand Xcode 4 to be installed and selected though, which is the only reason I have it. But if I remember correctly, purge also didn't work when I had Xcode 3 selected instead.
Privilege needed

On Mountain Lion purge can be run from a normal user account.

On Mavericks purge can only be run from root account (through sudo).
If you attempt to run it with a normal user account, you will get an error message:

Unable to purge disk buffers: Operation not permitted


Real function

purge doesn't act directly on main memory.
Its core function is to terminate all the IO pending which are using disk cache and then to
free all the occupied disk cache.

purge should free disk space to ease paging out and swapping out of main memory. Hence many people get convinced that purge was freeing main memory. This is an urban legend.

Real fault

This problem is clearly coming directly from the MacOS X kernel.
It might be caused by programs doing a lot of I/O on a system where the virtual memory is already heavily used. But these programs are false culprits.

You may find such a false culprit exhibited here: Mountain Lion Panic - decode

A kernel panic is a kernel fault. The type of error message is a kernel error message. It is a kernel fault.

From your actual context, I advise you to run sysdiagnose and open a bug case at Apple with the result. This tool will run for you all the relevant tools to analyze any misbehaviour of your system or anny application running on it.

Please read this professionnal answer: Apple sysdiagnose