Skip to contentcloudvix
cloudvix
GuideServers & hosting

Add swap on Ubuntu 24.04 and watch memory and disk

Denys DunaievskyiFounder, Cloudvix
  • 13 min read
  • Published
  • LevelBeginner
Guides
On this page

Key takeaways

  • Swap trades speed for not crashing: a process that would have been killed keeps running, slowly.
  • Use available, not free, 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.

On the serverbash
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.

On the serverbash
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
swapon --show
free -h
Warning

If 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.

On the serverbash
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 --show

Tell 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.

On the serverbash
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.conf

Read 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.

On the serverbash
free -h
df -h /
cat /proc/loadavg
vmstat 5 3
ps aux --sort=-%mem | head -n 6
Tip

Use 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).

On the serverbash
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.

On the serverbash
sudo du -xh --max-depth=1 /var | sort -h | tail -n 6
sudo journalctl --disk-usage

Remove 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.

On the serverbash
sudo swapoff /swapfile
sudo rm /swapfile
sudo sed -i '\|^/swapfile |d' /etc/fstab
swapon --show

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.

About the author

Denys Dunaievskyi

Founder, Cloudvix

Runs Cloudvix and is responsible for the guides published here.

Related guides

Your server is a few clicks away.

Choose the plan and the system in one order form; the location too, when there is more than one.

Create server