System freezes when RAM limit reached, doesn't use swap

I have a 16GB RAM system and whenever I hit the limit of RAM the whole system freezes even the mouse cursor starts to become jittery until it doesn't move at all. Keyboard, mouse, nothing. So I am forced to do a hard power-off which isn't probably good for the electronics. I've set up a 16GB swap which shows up properly in free -m and in top but it's always at 0 usage.

              total        used        free      shared  buff/cache   available
Mem:          15960        5823        3915         704        6220        9104
Swap:         16383           0       16383

Any ideas why my system doesn't want to use the swap to save me from a total crash?

More info:

$ grep -i swap /etc/fstab
/swapfile                                 none            swap    sw              0       0

$ sysctl vm.swappiness
vm.swappiness = 80

$ swapon -s
Filename                                Type            Size    Used    Priority
/swapfile                               file            16777212        0       -2

$ ls -al /swapfile
-rw------- 1 root root 17179869184 Jan 16  2021 /swapfile

Output of sudo lshw -C memory: https://pastebin.com/TGbjiLdm

I've played with the swappiness from 10 to 40 to 60 to 80 and it doesn't seem to make much difference.

Also, the system isn't hitting the limit very often. Only when I am editing very large video files with Kdenlive or running VirtualBox and things of that nature. I'm usually careful about it, but sometimes I accidentally hit that threshold of 16GB (with swap unused) and my system crashes.


Solution 1:

SWAP

Edit /etc/fstab...

sudo -H gedit /etc/fstab

And edit this line to remove tabs, and use only spaces...

/swapfile                                 none            swap    sw              0       0

Reduce your /swapfile... it's way too big...

Note: Incorrect use of the rm and dd commands can cause data loss. Suggest copy/paste.

In the terminal...

sudo swapoff -a           # turn off swap
sudo rm -i /swapfile      # remove old /swapfile

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

sudo chmod 600 /swapfile  # set proper file protections
sudo mkswap /swapfile     # init /swapfile
sudo swapon /swapfile     # turn on swap
free -h                   # confirm 16G RAM and 4G swap

Edit /etc/fstab, using sudo -H gedit /etc/fstab or sudo pico /etc/fstab.

Confirm this /swapfile line in /etc/fstab... and confirm no other “swap” lines... use SPACES in this line... confirm NO TABS...

/swapfile  none  swap  sw  0  0

reboot                    # reboot and verify operation

Reset your vm.swappiness...

Edit /etc/sysctl.conf...

sudo -H gedit /etc/sysctl.conf

Find the line that contains vm.swappiness=80 and change it to =60.

Save the file.

sudo sysctl -p

BIOS

ThinkPenguin Snares https://www.thinkpenguin.com/gnu-linux/snares-penguin-gnu-linux-notebook

You have a very old BIOS version 1.03.06 dated 06/25/2014. Tell me the EXACT make/model of your computer or motherboard, and we'll check for a newer BIOS.

Update #1:

Let's check your file system...

  • boot to a Ubuntu Live DVD/USB in “Try Ubuntu” mode
  • open a terminal window by pressing Ctrl+Alt+T
  • type sudo fdisk -l
  • identify the /dev/sdXX device name for your "Linux Filesystem"
  • type sudo fsck -f /dev/sdXX, replacing sdXX with the number you found earlier
  • repeat the fsck command if there were errors
  • type reboot

Update #2:

Initial failure during dd command. Freeze at login screen. fsck fixed some errors. Swap is now working correctly. BIOS appears to be old, but current. Runs ok when booted to Ubuntu Live DVD/USB. Needs Ubuntu reinstall.