Is there a way to programatically clear the buffer cache on the Mac, preferrably in C?
Basically, I'm looking for the equivalent of the source of 10.5 (and greater)'s purge
command. EDIT: I now see this is part of the CHUD tools, for which it seems the source isn't directly available. However, I'm still looking for some code to do the same.
Clearing your web browser cache is always safe. Your web browser stores files on your Mac to make browsing faster.
System cache, as you'd probably expect, is data created by macOS that helps your Mac run smoothly. Because it's connected to the operating system we would recommend against deleting any system cache, unless you are using a specific tool to do so.
In the Activity Monitor app on your Mac, click Cache (or use the Touch Bar). If you don't see the Cache tab in the Activity Monitor window, choose Apple menu > System Preferences, click Sharing, then select Content Caching. After that, quit and then reopen Activity Monitor to view Cache information.
I've disassembled the function in question (_utilPurgeDiskBuffers
) from the CHUD framework. The function doesn't seem to be very complex, but since I'm no MacOS programmer, the imports and called sys APIs don't make much sense to me.
The first thing the API does is to call another function, namely _miscUtilsUserClientConnect_internal
. This function seems to establish a connection to the CHUD kernel extension.
To do this, it calls _getCHUDUtilsKextService
which tries to locate the CHUD kernel extension by enumerating all kexts using the IORegistryCreateIterator
imported from the I/O kit. After the kext has been found, it is opened via _IOServiceOpen
.
At this point we have a connection to the CHUD kext (at least that's my understanding from the disassembly listing).
Finally a call to IOConnectMethodStructureIStructureO
is made, which I guess carries out the real magic.
Without knowing some internal details or the signature of this function the parameters don't make sense to me.
Here's the disassembly, though:
__text:4B0157A7 lea eax, [ebp+var_1C]
__text:4B0157AA mov dword ptr [esp+14h], 0
__text:4B0157B2 mov [esp+10h], eax
__text:4B0157B6 mov [esp+0Ch], eax
__text:4B0157BA mov dword ptr [esp+8], 0
__text:4B0157C2 mov dword ptr [esp+4], 0Eh
__text:4B0157CA mov [esp], edx
__text:4B0157CD call _IOConnectMethodStructureIStr
Note that var_1C
has been zeroed out before.
Hopefully some of you can make more sense out of those syscalls. If you want more information, let me know.
Update:
To get you started, just take the AppleSamplePCIClient.c
example from the IO kit SDK. This does basically what the purge application from the CHUD tools does.
The only thing you would have to change are the parameters to the final _IOConnectMethodStructureIStr
call. Take them from the disassembly listing above. I cannot test all this stuff since I don't have a Mac.
It seems that:
You can use usr/bin/purge (type purge in the terminal) to flush the disk cache (inactive memory), or you can do many random reads from the hard disk to do the same thing.
Taken from a comment from user guns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With