Add Swap Memory on Debian 10

Add Swap Memory on Debian 10

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

#/usr/sbin/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 SWAP FILE

Since the server as 512MB of RAM, we will create a 512MB file in this guide. Type the following command to create 512MB swap file (1024 * 512MB = 524288 block size):

#dd if=/dev/zero of=/swapfile bs=1024 count=524288

Sample outputs:

524288+0 records in
524288+0 records out
536870912 bytes (537 MB) copied, 3.23347 s, 166 MB/s

Where,

if=/dev/zero : Read from /dev/zero file. /dev/zero is a special file in that provides as many null characters to build storage file called /swapfile1.
of=/swapfile1 : Read from /dev/zero write storage file to /swapfile1.
bs=1024 : Read and write 1024 BYTES bytes at a time.
count=524288 : Copy only 523288 BLOCKS input blocks.

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
#/usr/sbin/mkswap /swapfile
Setting up swapspace version 1, size = 499996 KiB no label, UUID=a81f762c-ef8a-40b5-a845-52aed148aeea
#/usr/sbin/swapon /swapfile

Verify that the swap is available by typing:

#/usr/sbin/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

#vim /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

#vim /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

#vim /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