Add swap space to a server
This is a healthy server with 8G of RAM and 3GB of available RAM (
buff/cache is memory utilised by Linux to optimise performance.
It is still available RAM and can be used if necessary.
This is an unhealthy server with 4GB of RAM.
The available RAM (
avail Mem) is very low
buff/cache is also very low:
This is the unhealthy server after adding swap (using the instructions below):
You can see the 4GB of swap (
The server still has
601.7 avail Mem
3.5GB of swap is
freeand available if required.
I asked How do you decide how much swap to create?, Malcolm says, Same size as memory…
For a WordPress server, Malcolm suggests that you install a largeish swap, then if it uses too much memory it will just run slow rather than falling over… need to set the server params to make sure it doesn’t use swap unless absolutely necessary…
Examine existing swap config:
sudo swapon --show
Check available space:
Allocate file for swap and configure:
sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile
Edit fstab and apply the setting:
sudo cp /etc/fstab /etc/fstab.bak echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab sudo swapon --all
View the swapiness and cache pressure settings:
cat /proc/sys/vm/swappiness # probably displays 60 - reduce this to 10 (see below) # so that swap is only used if absolutely necessary cat /proc/sys/vm/vfs_cache_pressure # probably displays 100 - reduce this to 50 (see below) # to ease the pressure on RAM resources (unless RAM is plentiful).
/etc/sysctl.conf to set new values for these
- add these 2 lines to the end of the file:
Apply these settings:
sudo sysctl -p
View the settings using cat commands above or:
sudo sysctl -a
sudo swapoff -v /swapfile
Remove entry from
/swapfile none swap sw 0 0
sudo rm /swapfile