Swap
Memory is finite and can run out. In some cases, it would be best to fail and emit an OOM error to users. Alternatively, swap space can be used to keep a process running without failure visible to users.
Swap is inherently slower than memory. Furthermore the process of writing memory pages into swap (and eventually loading them back from swap) can make I/O a significant bottleneck. For these reasons and more, swap does carry a cost.
Contents
Setup
Create a new swap file.
# for 2GB sudo dd if=/dev/zero of=/swap bs=1024 count=2097152 sudo chmod 600 /swap sudo mkswap /swap
Add the file as swap.
sudo swapon /swap
To verify that the file is being used as swap, try:
swapon -s
And to make the swap persistent, add the following to fstab(5):
/swap swap swap defaults 0 0
Administration
Enable or Disable
To temporarily enable or disable swap, try:
sudo swapon /swap sudo swapoff /swap