Difference between revisions of "Swap"

From Freephile Wiki
Jump to navigation Jump to search
(add note about when to use dd)
Line 11: Line 11:
 
# dd if=/dev/zero of=/swapfile bs=1G count=2
 
# dd if=/dev/zero of=/swapfile bs=1G count=2
 
# fallocate is faster than dd because it doesn't actually write 2GB of zeroes
 
# 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
 
fallocate -l 2G /swapfile
 
# set permissions so that nobody but root can read/write
 
# set permissions so that nobody but root can read/write
Line 38: Line 39:
 
</source>
 
</source>
  
[[Category:System Administration]] [[Category:Cloud]]
+
[[Category:System Administration]]
 +
[[Category:Cloud]]

Revision as of 08:21, 20 October 2017

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.

# 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