4

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?

migrated from serverfault.com Jan 20 '17 at 18:19

This question came from our site for system and network administrators.

  • 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? – Julie Pelletier 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. – Julie Pelletier 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. – Julie Pelletier Jan 20 '17 at 18:36
8

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
  • 2
    chmod this file to 0600 for better security: sudo chmod 0600 /swapfile – Segfault Mar 17 '18 at 21:24
4

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

Your Answer

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