5

My Ubuntu 18.04 (Bionic Beaver) installation crashed because of a problem with out of memory.

I have messages like this:

Jan 31 16:56:53 adam-Lenovo-YOGA-720-15IKB kernel: [47980.843963] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/,task=chrome,pid=16144,uid=1000
Jan 31 16:56:53 adam-Lenovo-YOGA-720-15IKB kernel: [47980.843992] Out of memory: Killed process 16144 (chrome) total-vm:1229048kB, anon-rss:84920kB, file-rss:0kB, shmem-rss:140kB
Jan 31 16:56:53 adam-Lenovo-YOGA-720-15IKB kernel: [47980.895646] oom_reaper: reaped process 16144 (chrome), now anon-rss:0kB, file-rss:0kB, shmem-rss:140kB
Jan 31 17:07:20 adam-Lenovo-YOGA-720-15IKB kernel: [    0.000000] microcode: microcode updated early to revision 0xca, date = 2019-10-03
Jan 31 17:07:20 adam-Lenovo-YOGA-720-15IKB kernel: [    0.000000] Linux version 5.3.0-28-generic (buildd@lcy01-amd64-009) (gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)) #30~18.04.1-Ubuntu SMP Fri Jan 17 06:14:09 UTC 2020 (Ubuntu 5.3.0-28.30~18.04.1-generic 5.3.13)
Jan 31 17:07:20 adam-Lenovo-YOGA-720-15IKB kernel: [    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.3.0-28-generic root=UUID=e8808971-420e-45c4-96d7-2f490862bf41 ro quiet splash vt.handoff=1
Jan 31 17:07:20 adam-Lenovo-YOGA-720-15IKB kernel: [    0.000000] KERNEL supported cpus:

I have all the times a lot of programs running (Firefox with multiple windows and tabs, Chromium, Chrome, GIMP, Emacs, LibreOffice, and VMware Player for Windows 7).

Today I have 8 GB of RAM and 8 GB of swap file:

adam@adam-Lenovo-YOGA-720-15IKB:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:           7.5G        3.7G        351M        692M        3.4G        2.8G
Swap:          8.0G          0B        8.0G

Can we have some dynamic swap on Linux instead of limiting it to a preallocated size?

Swappiness:

adam@adam-Lenovo-YOGA-720-15IKB:~$ sysctl vm.swappiness
vm.swappiness = 10

3 Answers3

6

You watched a YouTube video that discussed the parameter vm.swappiness, and then set your vm.swappiness=10, which is totally wrong for your system with 8G RAM. That's why you're "running out of memory".

Set vm.swappiness=80 (based on 8G RAM and 8G SWAP), this way...

sudo -H gedit /etc/sysctl.conf # edit this file

Search for an existing vm.swappiness= entry...

CTRL+f vm.swappiness

  • If found, edit it to say vm.swappiness=80

  • If not found, add vm.swappiness=80 at the end of the file

Save your edits and quit gedit

sudo sysctl -p

Note: Based on what apps you're running, you might also be best served by adding more RAM, and changing vm.swappiness to a different value. To get the speed advantage of memory interleaving, do it in equal size RAM stick pairs, so in your case, add another 8G RAM stick (if you have one 8G RAM stick installed now). If you have two 4G sticks installed now, then you'll need two 8G RAM sticks.

heynnema
  • 73,649
5

An answer from Out of swap - what happens? explains dynamically sizing swap space is perilous and offers a way to test that hypothesis.

There was a package, SwapSpace, which you may run into, but it is no longer maintained.

An alternative dphys-swapfile service, detailed at Dynamically growing swap file on Debian may not be as dynamic as you need.

The solution from heynnema is superior and should be implemented before anything else.

K7AAY
  • 17,705
3

Naturally, adding more RAM is the best solution.

However, sometimes that's impractical, as it used to be for me. Because of that, I would sometimes enable a swap file when the system started to get really sluggish. That would give me some time to kill what I wanted killed.

https://linuxize.com/post/create-a-linux-swap-file/

  1. sudo fallocate -l 4G /swapfile # or whatever you want to name it, and wherever you want to place it.
  2. sudo chmod 600 /swapfile
  3. sudo mkswap /swapfile

Then, when the system gets sluggish, run sudo swapon /swapfile. When you deem it not necessary anymore, run sudo swapoff /swapfile.

EDIT: fallocate is know to work on ext4, but fail on XFS. dd if=/dev/zero of=/swapfile bs=1M count=4096 works on XFS.

RonJohn
  • 679