In the past I've gotten a bit fed up with out-of-memory conditions on Linux, where the virtual memory starts swapping and hogging disk activity, and the machine slows down.

So when I installed Ubuntu on my MacBook Pro, I noticed that it had 8 GB of memory, and I said to myself, "that seems like enough, I think I'll avoid swapping problems and not reserve a partition for virtual memory. I need the disk space anyways."

Well, to my surprise, it turns out that the user experience in out-of-memory conditions with Linux without virtual memory is much, much WORSE than I expected.

If I accidentally compile too many large C++ files at once (easy to do with "make -j6"), or something else that accidentally consumes the machine memory before I notice, instead of that program crashing and giving an error as I would expect, it turns out that the behaviour instead is that my entire desktop stops responding and I am forced to hard-reboot the computer! Sometimes I lose lots of time or work due to this!

I would fix it by going back and re-partitioning to give myself some virtual memory, but damn.. I can't afford to do that right now. Are there any tips for getting Linux to handle out-of-memory conditions more cleanly?

  • Possible duplicate of Linux OOM disk I/O. Also: swap, what is it good for?
    – kubanczyk
    Jan 20 '17 at 18:15
  • 1
    Nothing in the question indicates that it's actually running out of memory. On the contrary, it more likely seems to be clogging the CPU which makes your system unresponsive. If make -j6 is too much, why don't you simply reduce it? Jan 20 '17 at 18:34
  • Your question could be answered directly by showing you how to make a swap file but, based on my previous comment, it wouldn't help solving the real issue. Jan 20 '17 at 18:35
  • And please, for everyone's sake including your own, do not restart a computer that is actively working on what you asked it to do and then spend hours investigating why it became slow. It became slow because of what you did. Jan 20 '17 at 18:36

I would fix it by going back and re-partitioning to give myself some virtual memory, but damn..

You don't need to have a full partition dedicated to swap, and you don't need to re-partition.

Create swap as a file is pretty easy. Just create a large empty file, run mkswap on it, then add the swap.

# create an big empty 1GB file (or whatever size you like)
dd if=/dev/zero of=/swapfile bs=1M count=1024
# format the file as swap
mkswap /swapfile
# turn it on.
swapon /swapfile

If you want to make it permanent add it to your fstab

/swapfile swap swap defaults 0 0
  • 3
    chmod this file to 0600 for better security: sudo chmod 0600 /swapfile
    – Segfault
    Mar 17 '18 at 21:24

When I do large compilations (qtwebkit I'm looking at you) I use a control group to prevent swapping to death or other unfortunate side effects. For my 6G box I do this:

cgcreate -g memory:emerge
echo "4G" > /sys/fs/cgroup/memory/emerge/memory.limit_in_bytes
echo "4G" > /sys/fs/cgroup/memory/emerge/memory.memsw.limit_in_bytes
cgexec -g memory:emerge tmux

If an OOM condition occurs the only process that will get killed is the compilation.

  • Great tip! Do the limits apply to all child processes also? If so, does the parent process share it's memory quota with the child processes or is each process allowed to allocate 4G?
    – Segfault
    Mar 17 '18 at 21:19

You can also flag particular processes are more or less likely to be killed in low memory situations. By setting the oom_score_adj value on particular processes, you can control the priority in which they get killed.

By default all processes have a score of 0, and the score is from -1000 (never kill) to 1000 (always kill first).

Since Firefox is usually the biggest culprit of memory using on my machine, I start it with a script instead:

echo 900 > /proc/self/oom_score_adj
exec /usr/bin/firefox

This causes Firefox to launch with an OOM score of 900, which, as the only process with a score this high, means it will be killed immediately as soon as the system runs out of memory.

I also do a similar thing to set my desktop environment to a score of -900 so that it is least likely to be killed, because the defaults meant that half the time Firefox would use up memory and the desktop environment would get killed to free up memory, which then took out Firefox and every other program with it! Not great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy