Swap

From Freephile Wiki
Revision as of 10:40, 24 October 2017 by Freephile (talk | contribs) (link to RH blog)

(diff) ← Older revision | Approved revision (diff) | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Swap memory is a file-system based temporary storage for memory to allow a system to shuffle between different tasks that would otherwise consume all available physical memory (RAM). Cloud providers like AWS[1] and Digital Ocean[2] do not setup swap on their default images. So, it's up to you to enable swap. RedHat provides an overview of swap at https://www.redhat.com/en/blog/do-we-really-need-swap-modern-systems

# see if it's enabled?
swapon -s
# check how much RAM we have (also tells you swap is zero)
free -m
# how much disk do we have, and where?
df -h
# lets make a swap file that is 2x RAM
# dd if=/dev/zero of=/swapfile bs=1G count=2
# fallocate is faster than dd because it doesn't actually write 2GB of zeroes
# however, if you get an error (e.g. CentOS) when you get to 'swapon', then you'll need to physically create a file using dd
fallocate -l 2G /swapfile
# set permissions so that nobody but root can read/write
chmod 600 /swapfile
# setup the swap area
mkswap /swapfile
# turn it on
swapon /swapfile
# show it for confirmation
swapon -s
# make it permanent in the file system
echo /swapfile   none    swap    sw    0   0 | tee -a /etc/fstab
# there are some other problems with a default Droplet
# swappiness is too high, and cache pressure is too high

cat << HERE >> /etc/sysctl.conf
# swappiness for a VM should be closer to zero
# 60 might be good for a desktop
# 10 for a server
vm.swappiness=10
# you can check cache_pressure with
# cat /proc/sys/vm/vfs_cache_pressure
# Default is 100 at Digital Ocean which is bad
# set it lower
vm.vfs_cache_pressure=50
HERE