egregius.be

Urban Exploration, PHP and others…

macOS Catalina move caches and logs to RAMdisk

In a previous post I wrote about how I monitor the writes to several SSD’s.
Second post in this series is about macOS Catalina, how to use a RAM disk to store caches and logs so you can avoid unnecessary disk writes. Caches are used to speed up applications. For example a browser caches images, Javascript JS files, CSS stylesheets and more. RAM Memory is the ideal option for speed. In the previous post we saw that a RAM disk is at least 3 times faster than a NVME SSD. Those files are continuously updated and written to the disk, causing huge amounts of writes.

To avoid this I wrote a shell script that removes the cache folders, creates them on a RAM disk and creates symlinks for them so the applications don’t know it’s even happening.
The first file is the shell script that does the actual work:

[gist]1cfcb6025d62d082515ecd94bc1b8fa2[/gist]

That file is executed on system startup by a LaunchAgent, the file is stored as /Library/LaunchAgents/com.user.ramdisk.plist

[gist]2000dd08677df7bbf7cb0a2537e8b433[/gist]

Of course the LaunchAgent has to be enabled:

launchctl enable /Library/LaunchAgents/com.user.ramdisk.plist

After this, reboot your Mac and watch the RamDisk to see if everything’s working:

Within less than one hour it saved more than 700MB writes (The Google folder gets constantly written, the size doesn’t represent the real save).
Time to search how we can actually see how much is written to the RAM disk, there must be something we can do with iostat for that.

iostat -Id disk2
guy@Mars2020 ~ % sudo iostat -Id disk2
              disk2 
    KB/t xfrs   MB 
   24.37 153324 3648.93 

Instead of saving a meager 700MB, we saved 3648MB of disk writes.

If you have other folders that could be on a RAM disk let me know in the comments.