MacOS – way to set the disk caching behavior on macOS

big surmacosmemory

I have an 8GB RAM MacBook Pro that struggles under my workload and I regularly have yellow memory pressure. I do notice that during these times, the disk caching (i.e. 'Cached Files') is taking up ~1GB of RAM. Though I am well aware that files in RAM is much faster than if it has to pull it off the disk, the machine DOES have a pretty fast SSD and I'd be willing to have less disk cache in favor of more application RAM.

Is there a way to set the amount of memory (or behavior rules) around how the OS utilizes disk caching? I'm a bit surprised that when memory pressure gets bad, it still insists on using a good chunk of my RAM to cache instead of at least cutting that in half.

Best Answer

Yellow memory pressure is not in itself an indication of any sort of "problem" with your system.

If that is the only "negative" indication in Activity Monitor, then getting rid of cached files from memory won't help performance of your system. Rather the opposite.

Take a look at "Swap used" in Activity Monitor - if this is "0 bytes", then the system has the RAM it needs, and getting rid of cached files won't mean that the now freed memory can be used for something that would somehow speed up your system.

However, if your "Swap used" is a significant amount, it might be that getting rid of cached files could speed up your system. It doesn't mean that in every case though. What matters is whether the swapped out data is something the system actually needs now. If that is the case, you can check the amount of data swapped in/out per second (or minute or similar) to see if the system is actively swapping in/out data while you are experiencing problems with your workload.

If you do indeed convince yourself that changing the disk caching behavior is necessary, you can use the sysctl command in the Terminal to do so.

You can list all the possible settings using sysctl -a - especially the ones that have "vm" in the name are be interesting here. I would take a look at the following settings:

  kern.vm_page_free_min
  kern.vm_page_free_target
  kern.vm_page_free_reserved
  kern.vm_page_speculative_percentage
  vm.vm_page_background_mode
  vm.vm_page_background_target

You can see their current setting like this:

sysctl kern.vm_page_free_min

And set a new value with:

sysctl kern.vm_page_free_min=250000

Note that the above is just a fictive example - you would need to supply your own value.