Ubuntu 18.04, 1.5G Swap 100% used (32G RAM), soft RAID1. How to increase SWAP size?

I have a production server, recently, I have seen server becomes slow sometimes, and SSH is slow, and once get into the terminal, even press ENTER is slow.

When the server is OK, I checked the memory usage and CPU, CPU looks fine, RAM looks fine except SWAP is 100% used. I googled it seems SWAP should not be 100% used, maybe 50%.

So I would like to increase the SWAP size. But since this is a production server, and it has soft RAID1 configured, so I don't want to mess it up until I am sure it is absolutely correct.

Here are the server configs:

CPU is E3-1245-V2, 32G RAM, 3x2T HDD

top command result:

top - 05:33:54 up 2 days, 13:45,  1 user,  load average: 2.43, 2.43, 2.50
Tasks: 243 total,   1 running, 165 sleeping,   0 stopped,   0 zombie
%Cpu(s): 28.4 us,  0.6 sy,  0.0 ni, 69.0 id,  1.7 wa,  0.0 hi,  0.2 si,  0.0 st
KiB Mem : 32893852 total,   278960 free, 28153624 used,  4461268 buff/cache
KiB Swap:  1569780 total,        0 free,  1569780 used.  4226648 avail Mem 

df -h result:

Filesystem      Size  Used Avail Use% Mounted on
udev             16G     0   16G   0% /dev
tmpfs           3.2G  1.5M  3.2G   1% /run
/dev/md3         20G  7.4G   11G  41% /
tmpfs            16G     0   16G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs            16G     0   16G   0% /sys/fs/cgroup
/dev/md2        487M  148M  310M  33% /boot
/dev/md4        1.8T  536G  1.2T  32% /home
tmpfs           3.2G     0  3.2G   0% /run/user/1000

cat /proc/mdstat result:

Personalities : [raid1] [raid0] [raid6] [raid5] [raid4] [linear] [multipath] [raid10] 
md4 : active raid1 sdb4[1] sdc4[2] sda4[0]
      1931981760 blocks [3/3] [UUU]
      bitmap: 5/15 pages [20KB], 65536KB chunk

md2 : active raid1 sda2[0] sdb2[1] sdc2[2]
      523200 blocks [3/3] [UUU]
      
md3 : active raid1 sda3[0] sdb3[1] sdc3[2]
      20478912 blocks [3/3] [UUU]
      
unused devices: <none>

sudo swapon --show result:

NAME      TYPE      SIZE USED PRIO
/dev/sdb5 partition 511M 511M   -2
/dev/sdc5 partition 511M 511M   -3
/dev/sda5 partition 511M 511M   -4

I was going to follow this tutorial https://linuxize.com/post/how-to-add-swap-space-on-ubuntu-18-04/ but I am afraid since I have soft raid1 here, I am not sure if the instructions above still apply, or it should work.

Thanks!


The simplest is to create a swap file.

To create a file of suitable size. Based on your question, you only have a lot of free space available in /home, so it makes sense to place it there

sudo dd if=/dev/zero of=/home/.swapfile bs=1G count=10

The size is bs*count, in this case 10GB. Modify count to suit your needs. After creating the file (this could take a while), you'll have to properly format it and enable it:

sudo mkswap /home/.swapfile
sudo swapon /home/.swapfile

The new file should now be reflected in free -m.

To enable automatic use upon boot, add a line to the bottom of /etc/fstab:

/home/.swapfile  swap       swap  defaults                     0  0