Ubuntu Memory swap

· updated 31 May 2017

We can see if the system has any configured swap by typing:

sudo swapon --show

You can verify that there is no active swap using the free utility:

free -h

Check Available Space on the Hard Drive Partition:

df -h

Create a Swap File, an amount equal to or double the amount of RAM on your system is a good starting point.

sudo fallocate -l 1G /swapfile

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

ls -lh /swapfile

Enabling the Swap File, Make the file only accessible to root:

sudo chmod 600 /swapfile
ls -lh /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
free -h

Make the Swap File Permanent, enable swap at every reboot:

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

Adjusting the Swappiness Property:

# get current swappiness value by typing:
cat /proc/sys/vm/swappiness

# set the swappiness to 10
sudo sysctl vm.swappiness=10
# set this value automatically at restart
sudo nano /etc/sysctl.conf

At the bottom of the /etc/sysctl.conf file , you can add:

vm.swappiness=10

Adjusting the Cache Pressure Setting:

sudo sysctl vm.vfs_cache_pressure=50
# set this value at each restart
sudo nano /etc/sysctl.conf

At the bottom, add the line that specifies your new value:

vm.vfs_cache_pressure=50

Reference

how-to-add-swap-space-on-ubuntu-16-04