Add Swap Memory on Debian 10

Add Swap Memory on Debian 10

Checking no active swap with free -h and swapon --show command

#swapon --show
#free -h
             total       used       free     shared    buffers     cached
Mem:          492M       451M        41M        17M       125M       204M
-/+ buffers/cache:       120M       371M
Swap:           0B         0B         0B

No swap space active, then checking available space on the hdd

#df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda2       20G  1.8G   17G  10% /
udev             10M     0   10M   0% /dev
tmpfs            99M   13M   87M  13% /run
tmpfs           247M     0  247M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           247M     0  247M   0% /sys/fs/cgroup

Plenty of space available on the disk with /. ,Generally, an amount equal to or double the amount of RAM on your system is a good starting point.

Creating a Swap File with the fallocate program. Since the server as 512MB of RAM, we will create a 512MB file in this guide.

#fallocate -l 512MB /swapfile

We can verify that the correct amount of space was reserved by typing:

#ls -lh /swapfile
-rw-r--r-- 1 root root 489M Nov 3 06:56 /swapfile

Enabling the file as swap and swap space

#chmod 600 /swapfile
#mkswap /swapfile
Setting up swapspace version 1, size = 499996 KiB no label, UUID=a81f762c-ef8a-40b5-a845-52aed148aeea
#swapon /swapfile

Verify that the swap is available by typing:

#swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 488.3M 0B -1
#free -h
total used free shared buffers cached
Mem: 492M 456M 35M 17M 125M 205M
-/+ buffers/cache: 125M 367M
Swap: 488M 0B 488M

Our swap has been set up successfully

Making the Swap File Permanent, by adding the swap file to our /etc/fstab

#cp /etc/fstab /etc/fstab.bak
#echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab

Tuning Swap by Adjusting the Swappiness and Cache Pressure Setting

Set vm.swappiness and vm.vfs_cache_pressure value automatically by add the line to bottom of /etc/sysctl.conf

#nano /etc/sysctl.conf
vm.swappiness = 10
vm.vfs_cache_pressure = 50

Check the current swappiness value by typing:

cat /proc/sys/vm/swappiness
60

For a Desktop, a swappiness setting of 60 is not a bad value. For a server, you might want to move it closer to 0. Set the swappiness to a different value by using the sysctl.For instance set the swappiness to 10

#sysctl vm.swappiness=10
vm.swappiness = 10

Set value automatically by adding the line to bottom of /etc/sysctl.conf

#nano /etc/sysctl.conf
vm.swappiness=10

Save and close the file when you are finished.

Check the current Cache Pressure Setting

Configures how much the system will choose to cache inode and dentry information over other data.

#cat /proc/sys/vm/vfs_cache_pressure
100

Set this to a more conservative setting like 50 by typing:

#sysctl vm.vfs_cache_pressure=50
vm.vfs_cache_pressure = 50

Set value automatically by adding the line to bottom of /etc/sysctl.conf

#nano /etc/sysctl.conf
vm.vfs_cache_pressure=50

Save and close the file when you are finished.

Source : From https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-debian-10