Ubuntu 21.04 freezes

My Ubuntu 21.04 works OK after every booting & rebooting. Yet after 3-4 hours, it “freezes”. Nothing helps but force restart. How to avoid the latter?

The Terminal runs:

<free -h
              total        used        free      shared  buff/cache   available
Mem:          3.5Gi       2.2Gi       243Mi       278Mi       1.1Gi       832Mi
Swap:         2.0Gi        17Mi       2.0Gi

vm.swappiness = 60

Filename                Type        Size    Used    Priority
/swapfile                               file        2097148 17772   -2

top - 09:32:34 up 26 min,  1 user,  load average: 0.26, 0.59, 1.18
Tasks: 203 total,   3 running, 200 sleeping,   0 stopped,   0 zombie
%Cpu(s): 42.9 us,  0.0 sy,  0.0 ni, 57.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0top - 09:33:54 up 27 min,  1 user,  load average: 0.72, 0.73, 1.18
Tasks: 203 total,   3 running, 200 sleeping,   0 stopped,   0 zombie
%Cpu(s): 25.0 us, 18.8 sy,  0.0 ni, 56.2 id,  0.0 wa,  0.0 hi,  0.0 si, top - 09:33:54 up 27 min,  1 user,  load average: 0.72, 0.73, 1.18
Tasks: 203 total,   4 running, 199 sleeping,   0 stopped,   0 zombie
%Cpu(s): 40.0 us, 20.0 sy,  0.0 ni, 40.0 id,  0.0 wa,  0.0 hi,  0.0 si,top - 09:33:54 up 27 min,  1 user,  load average: 0.72, 0.73, 1.18
Tasks: 203 total,   2 running, 201 sleeping,   0 stopped,   0 zombie
%Cpu(s): 33.3 us, 33.3 sy,  0.0 ni, 33.3 id,  0.0 wa,  0.0 hi,  0.0 si,top - 09:33:54 up 27 min,  1 user,  load average: 0.72, 0.73, 1.18
Tasks: 203 total,   5 running, 198 sleeping,   0 stopped,   0 zombie
%Cpu(s): 28.6 us, 42.9 sy,  0.0 ni, 28.6 id,  0.0 wa,  0.0 hi,  0.0 si,
MiB Mem :   3623.3 total,    139.9 free,   2286.0 used,   1197.4 buff/c
MiB Swap:   2048.0 total,   2029.4 free,     18.6 used.    822.0 avail> 


# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
UUID=28539ebd-a6e3-4fa7-8dcc-8bdc31b0f505 /               ext4    errors=remount-ro 0       1
/swapfile                                 none            swap    sw 

To begin with, the most valuable stuff should be on a hard disc (e.g., Transcend) and that daily amended (having the Internet off). Keep the installation tool ready too. When freezing is bad and the advice is nowhere to get, then force switch off, plug in the installation tool, boot up while pressing F2. From a boot menu, chose the above tool and reinstall the system. Prefer the LTS version. Mind Livepatch. That worked in my case.


Solution 1:

You don't have enough RAM, or enough swap.

              total        used        free      shared  buff/cache   available
Mem:          3.5Gi       2.2Gi       243Mi       278Mi       1.1Gi       832Mi
Swap:         2.0Gi        17Mi       2.0Gi

We'll increase your swap from 2G to 4G and see if that helps...

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 4G 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

Update #1:

Reinstalled Ubuntu to fix the problem.