ACLClouds
ACLClouds

The VPS restarts or kills processes due to lack of memory

Confirm the OOM killer in the logs, identify the process that consumes the memory and fix its usage or resize the VPS to stop the restarts.

Linux can kill a process to protect the VM when there is no memory left. Kernel logs make it possible to distinguish between OOM, application crash and manual restart.

Step-by-step procedure

  1. Read the kernel logs

    Search for Out of memory and Killed process with journalctl or dmesg.

  2. Measure processes

    Use free -h, ps and the panel statistics to spot stable usage or a gradual leak.

  3. Limit services

    Reduce workers, cache, concurrency and the Docker memory limits. Avoid running several heavy databases on a small plan.

  4. Add a margin

    If the normal load remains too close to the limit, increase the RAM or distribute the services.

Memory diagnostics

free -h
ps aux --sort=-%mem | head -15
journalctl -k -g "Out of memory|Killed process" --since today

Create a 2 GB swap file

fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab
echo 'vm.swappiness=10' > /etc/sysctl.d/99-swap.conf
sysctl --system
swapon --show

Before you start

The "Statistics" page under "Additional tools" shows the memory used at the moment you open it. To find out what happened, read the kernel logs inside the VM: they keep the exact time and the name of the killed process.

The memory seen by Linux includes the disk cache. In free -m, look at the available column, not free: a low free is normal, an available close to zero is not.

Add a swap file

A swap file absorbs a short peak without the OOM killer stepping in. It does not replace RAM: used all the time, it slows the whole VPS down. Start with 1 to 2 GB, after checking free space with df -h.

  • The line /swapfile none swap sw 0 0 in /etc/fstab re-enables it at every boot.
  • vm.swappiness=10 in /etc/sysctl.d/99-swap.conf limits its use to shortage situations.

Check that everything works

swapon --show must list /swapfile. After a few hours of normal load, searching for Killed process in journalctl -k must return nothing and available must keep a margin.

Frequently asked questions

My application stops with Killed or Exit code 137, is it memory?

Very often. 137 means a stop by SIGKILL (128 + 9), sent by the OOM killer or by a Docker memory limit. dmesg | grep -i oom confirms it for the VPS; for a container, docker inspect --format '{{.State.OOMKilled}}' followed by its name returns true.

Node.js shows JavaScript heap out of memory, what should I do?

It is the V8 heap limit that is reached, not necessarily the VPS limit. If the RAM allows it, raise it with NODE_OPTIONS=--max-old-space-size=1024 (value in MB), staying below the available memory. If usage keeps growing, look for a leak instead: unbounded cache, growing arrays, listeners never removed.

Why is it always MySQL or Java that gets killed first?

The OOM killer picks the process that frees the most memory, so the biggest one, even if it did not cause the peak. Lower innodb_buffer_pool_size for MySQL or MariaDB, and keep the -Xmx of a JVM well below total RAM: the JVM also uses memory outside the heap.