I recently had data loss because the hard drive was completely used up by virtual memory and some programs couldn’t handle zero free space on the hard drive. How can I prevent this from happening (besides always keeping a few GB as a buffer)?
I can propose you one way to do that, although it is not complete.
You can set memory limit per process using
launchctl limit (see the man page). However this is per process not system wide.
In real life, few processes are consuming vast amount of memory. Therefore, I would advise you to look at
Activity Monitor output and see which are the process consuming the most. Count the ones that are using more than 20% of your RAM. How many such processes do you have? Let's assume n and assume that you have m GB of RAM. How much maximum swap space do you want to use? Let's assume s GB of swap.
I would do the following calculation:
(m + s - 1) / n = <max memory per process in GB>
Convert the GB into B:
<max memory per process in GB> * 1024 * 1024 * 1024 = <max mem per proc in B>
Now try this limit and see if it works (note, this setting will be lost after a reboot, so it is "safe" to perform):
sudo launchctl limit rss <max mem per proc in B> <max mem per proc in B + 1073741824>
If you are happy with this choice, then you can save them, so they are permanent. Edit the file
/etc/launchd.conf and add:
limit rss <max mem per proc in B> <max mem per proc in B + 1073741824>