I'm using ArchLinux, with 3.18.6-1 kernel, when a software uses more than physical memory (when total memory used is >5.8 GB), it always lags, unresponsive, and all that I can do is reset the computer. I have provide big enough swap on /etc/fstab

/swapfile swap swap defaults 0 0

My vm.swappiness is 1, the output of free -m command:

              total        used        free      shared  buff/cache   available
Mem:           7800        3059         688         239        4053        4204
Swap:         10247           0       10247

I tried to increase the swappiness to 10, then run a memory eating program munch.c but it always hangs when reach about 4000 MB

What's wrong with my Linux?

Or is there a way to automatically kill the program with hugest RAM usage? (that is likely the most recent running program, that is valgrind or my recent test program)

  • uh...changing vm.swappiness from 0 to 10 is a pretty marginal change. Try using 60 or 70 instead. – Bratchley Feb 20 '15 at 6:27
  • 1
    regarding the last part of your question, there are a variety of tunables for the OOM killer. vm.oom_kill_allocating_task=0 should kill off programs that are consuming a large amount of memory but there's no guarantee it'll be the one consuming the most (probably will be though). – Bratchley Feb 20 '15 at 6:30
  • 1
    Since you are using a file as swap, what kind of filesystem are you using in /? It may be a filesystem limit in the size of the files it can use. Why haven't you created a partition for swap? – YoMismo Feb 20 '15 at 8:16
  • I create a swap partition, but too small 2 GB, then I create a swapfile 8 GB, the / partition is ext4 – Kokizzu Feb 20 '15 at 8:47

I have experiment with echo 1 | sudo tee /proc/sys/vm/oom_kill_allocating_task, but it still lags for a few minutes before actually kill the problematic program. In my testing earlyoom is the best for this case, to install it, just type:

yaourt --needed --noconfirm -S --force earlyoom
sudo cp /usr/bin/earlyoom /usr/local/bin/
sudo systemctl enable earlyoom
sudo systemctl start earlyoom

Now try to compile and run this program again:

echo '
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv) {
    int max = -1;
    int mb = 0;
    char* buffer;
    if(argc > 1) max = atoi(argv[1]);
    while((buffer=malloc(1024*1024)) != NULL && mb != max) {
        printf("Allocated %d MB\n", ++mb);
    return 0;
' > munch.c && gcc -O2 -o munch munch.c 

It would give an output something like this:

Allocated 1 MB
Allocated 2 MB
Allocated 3 MB
Allocated 4367 MB
Allocated 4368 MB
Allocated 4369 MB

The program with highest memory usage now killed automatically and your system would be always responsive. To see the real time log of service type journalctl -f -u earlyoom it would show something like this:

-- Logs begin at Mon 2014-11-03 10:54:39 WIB. --
Feb 20 13:25:25 s497 earlyoom[20041]: earlyoom v0.3-15-g528196e
Feb 20 13:25:25 s497 earlyoom[20041]: total:  7800 MiB
Feb 20 13:25:25 s497 earlyoom[20041]: min:     780 MiB
Feb 20 13:25:25 s497 earlyoom[20041]: avail:  4963 MiB
Feb 20 13:33:10 s497 earlyoom[20041]: Out of memory! avail: 519 MiB < min: 780 MiB
Feb 20 13:33:10 s497 earlyoom[20041]: Killing process 24984 (munch)

press Ctrl+C to close that command.

  • Does this really explain why your system is hanging even when you're only using 5.8GB of 7GB? – Bratchley Feb 20 '15 at 6:51
  • no, but at least it stop hanging .___.)/|| – Kokizzu Feb 20 '15 at 6:55

I had the same issue on a few of my machines: It was as if Linux did not realize I had added swap.

The solution: Upgrade the kernel from 3.x to 4.x.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.