Swap and memory on your VPS

A small swap file on the NVMe disk is cheap insurance against out-of-memory crashes, but it does not replace RAM; the goal is that swap is touched rarely and the services you run are sized to the memory of your plan. This guide shows how to read memory use, add swap, and tune the usual memory-hungry programs.

How do you read memory use correctly?

Look at the available column, not free. Run:

free -h

Linux uses idle RAM as a disk cache, so free is often close to zero on a healthy server. available is the memory programs can still get, counting cache that can be dropped at once. A server with 4 GB, 300 MB free and 2.8 GB available is fine.

To see what uses the memory, sort processes by resident size:

ps -eo pid,comm,rss --sort=-rss | head -n 12

The RSS column is in kilobytes. htop gives the same view interactively; press F6 and pick PERCENT_MEM. For containers, docker stats --no-stream lists memory per container.

Does your server have swap already?

Check with:

swapon --show

If it prints nothing, there is no swap. Many cloud images ship without it, so a sudden spike, such as a big Composer install, a database import or a node build, can make the kernel kill a process. A swap file turns that crash into a short slowdown.

How much: 1~2 GB is enough on plans up to 4 GB of RAM, and 2~4 GB on larger ones. More rarely helps; if a server needs gigabytes of swap all day, it needs more RAM, not more swap.

How do you add a swap file?

Create the file, lock its permissions, format it and turn it on. This works on Ubuntu, Debian, Rocky, Alma, CentOS Stream and Fedora with ext4 or XFS:

fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile none swap sw 0 0" >> /etc/fstab

chmod 600 matters: a swap file can hold fragments of anything in memory, including passwords. The line in /etc/fstab turns it on again after every reboot. If fallocate fails on your file system, use dd if=/dev/zero of=/swapfile bs=1M count=2048 instead.

Verify with swapon --show and free -h; the Swap row should show 2.0Gi total. Test the fstab line without rebooting: swapoff /swapfile && swapon -a && swapon --show.

What swappiness value should you use?

10 on most servers. Swappiness tells the kernel how willing it is to move idle memory pages to swap rather than shrink the file cache. The default of 60 suits desktops; a lower value keeps your applications in RAM and uses swap mainly as an emergency reserve.

echo "vm.swappiness=10" > /etc/sysctl.d/99-swap.conf
sysctl --system
cat /proc/sys/vm/swappiness

The last command should print 10. For a pure database server you can go down to 1; avoid 0, which on modern kernels can make the OOM killer act before swap is used at all.

Is zram a better choice than a swap file?

zram compresses memory pages and keeps them in RAM, so a 4 GB server can hold roughly 5~6 GB of mostly idle data. It is faster than disk swap and saves NVMe writes, at the cost of some CPU. On Ubuntu and Debian:

apt install -y zram-tools
echo -e "ALGO=zstd\nPERCENT=50" > /etc/default/zramswap
systemctl restart zramswap

Fedora uses zram by default, and on Rocky and Alma you can install zram-generator and add /etc/systemd/zram-generator.conf with a [zram0] section. Both kinds can run together: zram gets the higher priority and the swap file catches the rest. swapon --show lists both with their priority.

On shared vCPU plans with busy neighbours, the CPU cost of compression is small; on the busiest workloads a dedicated CPU plan is the better fix anyway.

How do you tell that the OOM killer struck?

When memory and swap run out, the kernel kills the process it thinks frees the most memory, often the database. Services then restart or simply stop. Search the kernel log:

journalctl -k | grep -i -E "out of memory|oom-kill"
dmesg -T | grep -i killed

A line like Out of memory: Killed process 1234 (mysqld) tells you what was killed and when. To protect a vital service, lower its OOM score with a systemd override: run systemctl edit mariadb and add [Service] and OOMScoreAdjust=-500. That makes the kernel pick something else first, but the real fix is to size services to the RAM you have.

How do you size common services to your RAM?

Most out-of-memory problems come from defaults that assume a bigger machine. A few rules of thumb:

  • MySQL and MariaDB: innodb_buffer_pool_size about 40~50% of RAM on a server that also runs the web app, set in /etc/mysql/mariadb.conf.d/50-server.cnf or /etc/my.cnf.d/.
  • PostgreSQL: shared_buffers about 25% of RAM, and keep work_mem small (4~16 MB), since it is used per sort per connection.
  • PHP-FPM: pm.max_children equal to the memory you can give PHP divided by the size of one worker, often 40~80 MB. Measure with ps -o rss -C php-fpm8.3.
  • Java: set -Xmx explicitly, leaving at least 25% of RAM for the system and other programs.
  • Node.js: --max-old-space-size in MB caps the heap of each process.

After a change, restart the service and watch free -h at the busiest hour of the day.

When is it time for more RAM?

When swap is in use all day, not only during spikes. Check with vmstat 5: the si and so columns show pages swapped in and out every five seconds. Zeros with a rare burst are healthy; steady numbers mean the working set no longer fits. At that point move to the next size, for example from S4 to S8, where memory costs about $0.79 per GB a month. The move guide shows how to do it with minimal downtime.

Questions about this guide

Does swap wear out the NVMe disk?

Not with occasional use. Swap that is touched during spikes writes very little; constant heavy swapping is a sign to add RAM, which also protects the disk.

Is swap enabled on new servers?

Check with swapon --show after the first login. If it prints nothing, add a swap file as shown above; it takes about a minute.

Can I add RAM without moving the server?

No, plans are not resized in place. Order the larger plan and move your data, as described in the move guide.

What is the cheapest way to buy more memory?

Shared vCPU plans have the lowest price per GB: S4 works out at about $1.06 per GB of RAM a month, and the larger shared sizes keep that rate low.

Why does free show almost no free memory?

Linux fills idle RAM with disk cache and gives it back as soon as a program needs it. Read the available column instead of free.

Should I drop caches to free memory?

No. Commands like echo 3 > /proc/sys/vm/drop_caches only slow the server down for a while; the kernel manages the cache better on its own.