Caches and buffers are everywhere in computing and computers, and swap space is one of the more significant uses of caching. They’re often confused, as there is overlap in their meaning and use.
In general, a buffer provides temporary storage, like a reservoir, to cope with transfers where one or more steps are significantly slower than others. A buffer is normally simply managed as a ‘first in, first out’ queue. For example, many printers have limited memory, so when printing large and complex pages, each will normally be written to a print buffer to await transfer to the printer, allowing printing software to complete its task well before all the pages have been sent to the printer.
Caches are normally more complex, and don’t just act as simple reservoirs, but provide fast-access local storage to save having to wait for data access over a slower connection. When your browser loads a web page from a remote server, the slowest step is obtaining the page from the server, so the browser stores pages locally, some in memory and others in temporary files on disk. When you move back to the previous page in your browser, it doesn’t have to fetch that page again from the server, but simply reloads it from its cache.
Caches are used extensively in hardware, most importantly in processors. Even in an Apple silicon Mac, accessing main memory is relatively slow, so all processors include one or more levels of high-speed cache memory to accelerate their performance.
Swap space is dedicated storage on disk used by memory management to cache pages of memory that haven’t been accessed recently. In recent macOS, it has its own hidden volume named VM in the active boot volume group. It’s only used when macOS memory management needs to free up physical memory. Activity Monitor’s Memory view shows the current Swap Used at the bottom of its window. Regular use of significant amounts of swap space can slow down your Mac, and might in some circumstances lead to more rapid ageing of an SSD hosting the VM volume. While older versions of macOS could be configured to store swap on a different disk, that’s no longer possible with a current APFS boot volume group.
Caches are also used throughout macOS, and other operating systems, to improve performance. At their simplest, a database might cache information about an app, such as whether that version has undergone full security checks, sparing those checks from being repeated every time that app is opened. Rosetta 2 caches ARM code it has translated from Intel binaries to save time when you want to run an Intel app on an Apple silicon Mac. Browsers and their supporting systems are among the most complicated, as they cache site information, pages and their contents, and probably trust evaluations in the right circumstances.
Because a cache succeeds by saving the time and trouble of repeatedly obtaining data, one of the critical variables for any caching scheme is how long it continues to re-use cached data before refreshing it. Some data on web pages, such as images, is unlikely to change from one week to the next, while text content can be replaced within minutes. Striking the right balance isn’t easy, and some browsers opt for performance rather than ensuring their caches haven’t become stale, which can become irritating.
macOS tends to hide its caches away to discourage users and software from tampering with them. A peek in one of the more obvious locations ~/Library/Caches will probably reveal several GB of cache files, many of which the client apps are blissfully unaware of. For example, SilentKnight, which doesn’t itself perform any caching in its code, has accumulated 2.3 MB of cached data in its folder there. I have absolutely no idea what might be in those files, as that caching is performed by macOS, not the code that I write.
Another favourite location for caches is in /var/folders, stored on the Data volume, where opaquely named folders are full of mysterious files, amounting to another several GB of unknown data.
Individual apps that rely heavily on caching, such as web browsers, often contain commands to empty or flush their caches. In Safari 16, this is provided in its hidden Develop menu. To enable that, open its Settings, select the Advanced section, and tick the box at the foot of the window. Beware, though: emptying its caches is an all-or-nothing command that empties the whole lot, except of course the one that you most want emptied. In most cases, you may prefer to clear its history, the command at the foot of its History menu, instead.
You can also flush macOS system caches more generally, including font caches and others which can cause trouble at times. The way to do this is to start up in Safe mode, using the Shift key on Intel Macs or through Recovery mode on Apple Silicon models. This is a valuable procedure that often fixes all manner of glitches that can occur following a macOS update, for example. However, it shouldn’t be considered routine maintenance.
So whatever you do in macOS and apps, chances are that something, somewhere is going to be caching it. And one day, that caching will catch up with you and result in something puzzling or downright annoying. But without all that caching, our Macs would be irritatingly slow: that’s the tradeoff.