Swap: Difference between revisions

From Freephile Wiki
No edit summary
No edit summary
 
Line 3: Line 3:


== Procedure ==
== Procedure ==
Here's a procedure that you can run as root, or an administrative user with sudo privileges.
Here's a procedure that you can run as root, or an administrative user with sudo privileges. If you want more info, or want to '''modify''' your swapfile, see [https://askubuntu.com/questions/927854/how-do-i-increase-the-size-of-swapfile-without-removing-it-in-the-terminal the AskUbuntu thread here]
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# see if it's enabled?
# see if it's enabled?
Line 12: Line 12:
df -h
df -h
# lets make a swap file that is 2x RAM
# lets make a swap file that is 2x RAM
# 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
# however, as of 2025 it is still best to NOT write sparse files, and therefore use 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
chmod 600 /swapfile
chmod 600 /swapfile

Latest revision as of 11:35, 30 October 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. If you want more info, or want to modify your swapfile, see the AskUbuntu thread here

# 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, as of 2025 it is still best to NOT write sparse files, and therefore use 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