Skip to content
DevOps devops linux-admin 6 min read

Memory & Swap Management

Every Linux server has a fixed amount of RAM (Random Access Memory — the fast, temporary storage your programs run in). When your apps ask for more memory than the machine has, Linux has two choices: borrow some slow disk space as “fake” RAM, or kill a process to free memory. This page shows you how to read your memory usage clearly, and how to add swap space so a small server does not crash when it runs low on RAM. This matters most on a VPS (Virtual Private Server — a small rented cloud machine) that often ships with just 1 GB or 2 GB of RAM.

Checking memory with free

The free command shows how much memory is used and available. The -h flag means “human-readable” (it prints GB and MB instead of raw bytes).

free -h

Output:

               total        used        free      shared  buff/cache   available
Mem:           1.9Gi       742Mi       180Mi        12Mi       1.0Gi       980Mi
Swap:             0B          0B          0B

Beginners often panic when they see “free” is low. Don’t. The number that actually matters is available, not free. Here is what each column means:

ColumnMeaning
totalAll the RAM installed on the machine.
usedRAM actively held by running programs.
freeRAM that is completely unused (often small — that’s normal).
buff/cacheRAM Linux is using as a disk cache (a copy of recently read files to speed things up). This is reclaimable.
availableThe realistic amount of RAM a new program could use right now. Watch this number.
SwapDisk space used as overflow when RAM fills up (0 here means no swap is configured).

The key insight: Linux deliberately fills spare RAM with buff/cache because empty RAM is wasted RAM. If a program needs that memory, Linux instantly frees the cache. So available already accounts for reclaimable cache — that’s why it is much larger than free.

Tip: If available stays consistently near zero and Swap is also full, your server is genuinely out of memory and at risk of the OOM killer (see below). That is your real warning sign — not a low free value.

What is swap, and when to use it

Swap is disk space that Linux uses as a slow extension of RAM. When RAM fills up, the kernel moves rarely-used memory pages out to swap, freeing real RAM for active work. Without swap, when RAM runs out the kernel invokes the OOM killer (Out Of Memory killer — a part of the kernel that force-kills a process to free memory), which often takes down your database or app with no warning.

When to use swap:

  • On small VPS instances (1-2 GB RAM) that occasionally spike — swap absorbs the spike instead of crashing.
  • As a safety net for build steps or batch jobs that briefly need extra memory.

When NOT to rely on swap:

  • As a permanent substitute for real RAM. Swap is on disk and is far slower than RAM; an app running heavily from swap will be sluggish.
  • On the database server itself for hot data — heavy swapping kills database performance. Size the RAM correctly instead.

Creating a swap file step by step

Older guides used a separate disk partition for swap, but a swap file (an ordinary file used as swap) is simpler, resizable, and works fine on modern Ubuntu (22.04 / 24.04 LTS). Here is the full procedure.

Step 1 — Create the file

Use fallocate to instantly reserve a file of the size you want. A common rule of thumb is swap = your RAM size (so 2 GB here).

sudo fallocate -l 2G /swapfile

Step 2 — Lock down its permissions

The swap file can contain sensitive memory contents, so only root should read it.

sudo chmod 600 /swapfile

Step 3 — Format it as swap

mkswap writes a swap signature so the kernel knows the file is swap space.

sudo mkswap /swapfile

Output:

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=8c5b1f0a-2d3e-4a91-9c77-1f2e3d4b5a6c

Step 4 — Turn it on

swapon activates the swap immediately, with no reboot needed.

sudo swapon /swapfile

Verify it is active:

free -h

Output:

               total        used        free      shared  buff/cache   available
Mem:           1.9Gi       760Mi       150Mi        12Mi       1.0Gi       960Mi
Swap:          2.0Gi          0B       2.0Gi

The Swap row now shows 2.0 GiB. You can also confirm with swapon --show:

swapon --show

Output:

NAME      TYPE  SIZE USED PRIO
/swapfile file    2G   0B   -2

Step 5 — Make it survive reboots

So far the swap only lasts until the next reboot. To make it permanent, add a line to /etc/fstab (the file that lists filesystems and swap to mount at boot).

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Gotcha: Always back up /etc/fstab first (sudo cp /etc/fstab /etc/fstab.bak). A typo in this file can stop the server from booting. After editing, test it without rebooting by running sudo swapoff /swapfile && sudo swapon -a — if that succeeds with no error, your fstab entry is valid.

Tuning swappiness

Swappiness is a kernel setting (0-100) that controls how eagerly Linux moves memory to swap. A high value swaps early and often; a low value keeps data in RAM until it really has to swap. The Ubuntu default is 60, which is fairly aggressive for a server.

Check the current value:

cat /proc/sys/vm/swappiness

Output:

60

For most servers, a lower value like 10 keeps your app in fast RAM and only uses swap as a last resort. Set it live (this resets on reboot):

sudo sysctl vm.swappiness=10

To make it permanent, add it to a sysctl config file:

echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
Swappiness valueBehaviourGood for
60 (default)Swaps fairly eagerlyDesktops, general use
10Swaps only when RAM is nearly fullMost web/app servers
1Swap is an absolute last resortLatency-sensitive databases
0Avoid swap until truly out of RAMRarely needed; risk of OOM kills

Removing swap

If you ever need to remove the swap file, turn it off, delete the fstab line, then delete the file:

sudo swapoff /swapfile
sudo sed -i '\#/swapfile#d' /etc/fstab
sudo rm /swapfile

Best Practices

  • Trust the available column in free -h, not free — Linux caching makes free look misleadingly low.
  • Add at least a small swap file (1-2 GB) on any VPS with 2 GB RAM or less to avoid sudden OOM crashes.
  • Treat swap as a safety net, not a substitute for adequate RAM — heavy, constant swapping means you need a bigger machine.
  • Lower vm.swappiness to around 10 on app and web servers so hot data stays in fast RAM.
  • Always back up /etc/fstab before editing, and validate with sudo swapon -a before you trust a reboot.
  • Set chmod 600 on the swap file so its memory contents are not world-readable.
Last updated June 15, 2026
Was this helpful?