On this page
Key takeaways
- Swap trades speed for not crashing: a process that would have been killed keeps running, slowly.
- Use
available, notfree, to judge memory. - A broken line in this file can slow down the next boot, so do not skip that check.
Swap is disk space the kernel uses as an emergency overflow when memory runs out. It will not make a small server fast, but it can turn a killed process into a slow moment. This guide adds a swap file to Ubuntu 24.04, makes it survive a reboot and then shows the five numbers worth looking at to tell whether the server is short of memory or disk.
Before you start
You need an Ubuntu 24.04 server and a user with sudo, as in the first-hour guide. Nothing here restarts the server or stops a service. The one thing to decide is the size: 2 GB is a sensible start for a small server, and the file needs that much free disk space.
- an Ubuntu 24.04 server and a user with sudo
- at least 5 GB of free disk space (the first command shows it)
- five minutes; no restart is needed
The steps
See what you have
Look at three things. free -h shows memory and swap in human units. swapon --show lists active swap and prints nothing if there is none. df -hT / shows free disk space and the file system type: ext4 and xfs work with the next step. If the type is btrfs, stop and read the Ubuntu swap documentation, a swap file needs special handling there.
free -h
swapon --show
df -hT /Create the swap file
fallocate reserves 2 GB at once, chmod 600 makes the file readable by root only (swap can contain memory contents, and swapon complains about looser rights), mkswap writes the swap format and swapon starts using it. The last two commands must show the file and about 2 GB of swap.
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
swapon --show
free -hIf fallocate answers that the operation is not supported, create the file with sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress and continue with chmod.
Keep it after a reboot
The first command saves a copy of /etc/fstab, the file that lists what is mounted at boot. The second adds one line for the swap file. findmnt --verify checks the file for mistakes (it may also report problems that were there before; read only the lines about /swapfile). The last three prove that the line works: they turn the swap off and start it again from fstab. A broken line in this file can slow down the next boot, so do not skip that check.
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo findmnt --verify
sudo swapoff /swapfile
sudo swapon -a
swapon --showTell the kernel to prefer memory
vm.swappiness says how willing the kernel is to move memory to swap. The default is 60. A lower value such as 10 keeps programs in memory for longer and uses swap only when memory gets short, which is what you want on a server. This setting is stored in its own file, so it also survives a reboot.
cat /proc/sys/vm/swappiness
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swap.conf
sudo sysctl -p /etc/sysctl.d/99-swap.confRead five numbers
Run these when something feels slow. free -h: how much memory is left in the column available (not free). df -h /: how full the disk is. cat /proc/loadavg: the three load averages; a value far above the number of processor cores means a queue. vmstat 5 3: ignore the first line, an average since boot; in the other lines the columns si and so count memory swapped in and out, and if they stay above zero, the server is swapping all the time. The last command lists the five programs that use the most memory.
free -h
df -h /
cat /proc/loadavg
vmstat 5 3
ps aux --sort=-%mem | head -n 6Use available, not free, to judge memory. Ubuntu keeps unused memory as file cache, which is given back at once when a program needs it.
Look for programs the kernel has killed
When memory and swap are both full, the kernel stops a process to save the server. The log of that is in the kernel messages. _TRANSPORT=kernel reads them across all boots in the period; the shorter journalctl -k would show only the current boot and miss an event before a restart. No output means that nothing was logged in seven days, as far as the journal reaches back (journalctl --list-boots shows how far).
sudo journalctl _TRANSPORT=kernel --since "7 days ago" | grep -i -E 'out of memory|oom-kill|killed process'Find what fills the disk
The first command shows the largest folders under /var, where logs, caches and container data usually live (-x stays on one file system). The second shows how much the system journal takes.
sudo du -xh --max-depth=1 /var | sort -h | tail -n 6
sudo journalctl --disk-usageRemove the swap file
This turns the swap off, deletes the file and removes its line from fstab (the sed command deletes the line that starts with /swapfile ). The copy /etc/fstab.bak from the earlier step is your way back if something looks wrong.
sudo swapoff /swapfile
sudo rm /swapfile
sudo sed -i '\|^/swapfile |d' /etc/fstab
swapon --showLet an assistant do it, with a plan first
If you use an AI assistant that can run SSH commands, paste the prompt below. It works with SSH access as a user with sudo. If the assistant should also order the server, use a Cloudvix API key, which has limits (up to €500 a month and 25 servers), instead of your account login.
The prompt makes the assistant look first, show a plan before it acts, check each step and stop when a check fails. It must not stop services or kill programs; it only reports what uses the memory.
You are helping me add swap to an Ubuntu 24.04 server at SERVER_IP and check its memory and disk. Connect over SSH as the user ops, which has sudo.
Rules:
1. Before you change anything, show me a numbered plan and wait for my yes.
2. After every step, run a check command and show me its output.
3. If a check fails, stop and tell me. Do not try to work around it.
First only look: run free -h, swapon --show and df -hT / and tell me what you see. Plan to propose after that, only if there is no swap and the file system is ext4 or xfs with at least 5 GB free: create a 2 GB swap file /swapfile with mode 600, start it with mkswap and swapon, back up /etc/fstab to /etc/fstab.bak and add the line /swapfile none swap sw 0 0, prove the line works with swapoff /swapfile followed by swapon -a, set vm.swappiness=10 in /etc/sysctl.d/99-swap.conf; then report free -h, the five programs that use the most memory and any out-of-memory events of the last seven days (use journalctl _TRANSPORT=kernel --since "7 days ago", not journalctl -k, which shows only the current boot).
Do not stop services, do not kill processes and do not restart the server. At the end, list what you changed and how to undo each change.If something goes wrong
fallocate failed: Operation not supported
The file system does not allow the quick method. Create the file with dd as shown in the callout of the second step; it takes a little longer and works on more file systems.
swapon: /swapfile: insecure permissions 0644
The file can be read by others. Run sudo chmod 600 /swapfile and start the swap again. This is a warning, but fix it: swap can hold copies of passwords and keys that were in memory.
The swap is gone after a reboot
The line in /etc/fstab is missing or has a typo. Look at it with grep swap /etc/fstab; it must read /swapfile none swap sw 0 0. Then run sudo swapon -a and check swapon --show.
A program still gets killed with swap on
Look at the memory log in the sixth step and at ps aux --sort=-%mem. If one program needs more than the memory and swap together, swap cannot help; that program, or the server, needs more memory.
Questions people ask
How big should the swap file be?
For a small server, 1 to 2 GB is a common start, and the exact figure matters less than people think: swap is a safety net. If you need a lot more than the memory you have, the better answer is a larger plan.
Does swap make the server faster?
No. Reading from disk is far slower than reading from memory. Swap trades speed for not crashing: a process that would have been killed keeps running, slowly. If vmstat shows swapping all the time, the server is too small for its job.
When should I choose a bigger plan instead?
When the si and so columns stay above zero for long stretches, when the memory log shows killed programs, or when available is close to zero in normal use. The guide on plan sizes explains how to compare them.
Where to go next
If the numbers say the server is too small, pick a size with the plan guide, and keep the unattended updates guide in mind: a restart after a kernel update empties the swap.
Runs Cloudvix and is responsible for the guides published here.