How to prevent OSX from allocating as much virtual memory as there is space available on the hard drive? 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 = &lt;max memory per process in GB&gt;

Convert the GB into B: &lt;max memory per process in GB&gt; * 1024 * 1024 * 1024 = &lt;max mem per proc in B&gt;

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 &lt;max mem per proc in B&gt; &lt;max mem per proc in B + 1073741824&gt;


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 &lt;max mem per proc in B&gt; &lt;max mem per proc in B + 1073741824&gt;