Swap: Difference between revisions
From Freephile Wiki
No edit summary |
No edit summary |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
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]]<ref>https://serverfault.com/questions/218750/why-dont-ec2-ubuntu-images-have-swap</ref> and [[Digital Ocean]]<ref>https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04</ref> do not setup swap on their default images. So, it's up to you to enable swap. | 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]]<ref>https://serverfault.com/questions/218750/why-dont-ec2-ubuntu-images-have-swap</ref> and [[Digital Ocean]]<ref>https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04</ref> do not setup swap on their default images. So, it's up to you to enable swap. RedHat provides [https://www.redhat.com/en/blog/do-we-really-need-swap-modern-systems an overview of swap] where you can learn the basics of Virtual Memory Management, paging, swapping, and thrashing. | ||
< | |||
== Procedure == | |||
Here's a procedure that you can run as root, or an administrative user with sudo privileges. | |||
<syntaxhighlight lang="bash"> | |||
# see if it's enabled? | # see if it's enabled? | ||
swapon -s | swapon -s | ||
Line 11: | Line 14: | ||
# 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 21: | Line 25: | ||
swapon -s | swapon -s | ||
# make it permanent in the file system | # make it permanent in the file system | ||
echo /swapfile none swap sw 0 0 | tee -a /etc/fstab | echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab | ||
# there are some other problems with a default Droplet | # there are some other problems with a default Droplet | ||
# swappiness is too high, and cache pressure is too high | # swappiness is too high, and cache pressure is too high | ||
cat << HERE | cat << HERE | sudo tee -a /etc/sysctl.d/10-swap.conf | ||
# swappiness for a VM should be closer to zero | # swappiness for a VM should be closer to zero | ||
# 60 might be good for a desktop | # 60 might be good for a desktop | ||
Line 36: | Line 40: | ||
vm.vfs_cache_pressure=50 | vm.vfs_cache_pressure=50 | ||
HERE | HERE | ||
</ | </syntaxhighlight> | ||
== Feature == | |||
This is the subject of a feature request https://github.com/freephile/meza/issues/176 | |||
[[Category:System Administration]] [[Category:Cloud]] | [[Category:System Administration]] | ||
[[Category:Cloud]] |
Latest revision as of 13:14, 13 August 2025
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 where you can learn the basics of Virtual Memory Management, paging, swapping, and thrashing.
Procedure
Here's a procedure that you can run as root, or an administrative user with sudo privileges.
# 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' | sudo 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 | sudo tee -a /etc/sysctl.d/10-swap.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
Feature
This is the subject of a feature request https://github.com/freephile/meza/issues/176