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)?
How to prevent OSX from allocating as much virtual memory as there is space available on the hard drive?
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>
@bmike I am sorry but can't you put your comments in your own answer or comment? I am not sure the ton you use fits with mine. Although valuable, I will ask for you edit to be undone. Feel free to add it as a comment of my answer or as a plain answer. Feb 24, 2013 at 16:23
Just click edit and revert my changes - do be aware that the site is very open to more than one contributor to any post. What I proposed is an answer component and not a comment IMO, but I'll probably copy your words and add mine if that makes you feel better. I figured you did the bulk of the work and wanted the authorship to remain yours, but I'm open to leaving your entry as is and adding a second answer.– bmike ♦Feb 24, 2013 at 16:38
I know, I also did set some of my post as community wiki. But this time I felt that the style of our answers were too different, and it felt awkward. Thank you, I did found that one can revert changes! Great tool! Feb 24, 2013 at 17:49
@Huygens a bit confused, shouldn't one multiply when converting from GB to byte? Aug 7, 2018 at 1:52
@FrancisLaclé indeed! You are right. I will edit the answer and correct that. A 5y+ old bug somewhat ;-) Sep 2, 2018 at 20:13