Find the process eating your VPS RAM before you buy a bigger server, because the fix is often a setting, not a size. A 2 GB VPS that runs out of memory usually has one greedy process behind it: a database with a buffer pool that is too big, a PHP-FPM pool with too many workers, or a runaway job that never ends. This guide shows how to find that process with three commands, read the numbers correctly, and stop the real culprit without breaking the rest of the server.
Start with the simplest check. Run free -h and read the available column. Linux uses spare memory as disk cache, so the free column is almost always low on a healthy server. If available is under 200 MB, the server is close to trouble. If it is zero, the kernel is already swapping or killing processes, and finding the culprit is urgent.
The Process Eating Your VPS RAM: Sort with ps
The fastest way to name the culprit is ps sorted by memory:
ps aux --sort=-%mem | head -15
The output lists every process by memory use, biggest first. The columns to read are %MEM and RSS. RSS is the resident memory: the RAM the process actually holds right now. A mysqld row at 600 MB on a 2 GB server is the first suspect. Fifteen php-fpm rows at 40 MB each are the second: no single worker looks big, but together they hold 600 MB.
Read the COMMAND column carefully before you act. A process named mysqld is the database. A process named php-fpm: pool www is a PHP worker. A process named rsync or tar is a backup that will finish on its own. Killing a backup to save memory just means running it again later, while killing a database loses every open connection. The name tells you whether the memory use is normal work or a problem.
Run the same command twice, a minute apart. A process that grows between the two runs is leaking memory or accumulating work. A process that stays the same size is just big. That distinction decides the fix: a leak needs a restart or a code fix, while a big-but-stable process needs a smaller setting.
htop: The Live View That ps Cannot Give
ps is a snapshot. htop is the live view, and it shows what a snapshot misses: whether memory use is rising, falling, or stable, and whether the CPU is involved. Open it and press M to sort by memory:
htop
Press H to collapse threads into their parent process. Without that, a MySQL server shows as dozens of threads and the numbers are hard to read. With threads collapsed, each service is one row, and the total is obvious at a glance. The memory bars at the top tell the same story in color: green is used, blue is buffers, yellow is cache. A bar that is mostly green with no yellow means real memory pressure. A bar that is mostly yellow means the disk cache is doing its job.
For a longer view, this guide to monitoring disk, RAM, and CPU usage with htop and Glances shows how to watch the server over hours instead of seconds, and this walkthrough of checking VPS resource usage with htop, df, and free covers the basics of reading the numbers. Both are worth a read if htop is new to you.
RSS, Shared Pages, and Why the Numbers Disagree
The numbers from different tools disagree, and the reason is shared memory. A PHP-FPM worker that shows 40 MB of RSS may share 20 MB of that with other workers through OPcache, so fifteen workers do not really use 600 MB. The tool that shows the honest number is smem, which splits shared pages fairly between the processes that use them:
sudo apt install -y smem
smem -t -k -s pss
The PSS column is the proportional set size: each process gets its fair share of shared memory. Sort by it, and the ranking changes: the database usually stays on top, while the PHP workers drop down the list. Use PSS when you need the true total, and RSS when you need a quick look.
Another number that confuses people is VSZ, the virtual size. It counts memory the process has mapped but may never touch, including shared libraries and allocated-but-unused space. A Java app with a VSZ of 4 GB on a 2 GB server is not using 4 GB; it is reserving address space. Ignore VSZ for memory diagnosis and read RSS or PSS instead.
The kernel’s own view is in /proc/meminfo, and the two lines worth reading are MemAvailable and Slab. A large Slab means the kernel itself is holding memory for caches and structures, which happens on servers with many small files or many network connections. If Slab is over 300 MB on a 2 GB VPS, a reboot clears it, and the long-term fix is usually fewer files in one directory or a connection limit.
Stop the Culprit Safely
Once you know the process, fix the cause instead of killing the symptom. A database that uses too much memory needs a smaller buffer pool, not a restart. This guide to MySQL 8.4 tuning on a 2 GB VPS shows the right values for a small server. A PHP-FPM pool with too many workers needs a smaller pm.max_children, not a kill signal. This guide to PHP-FPM tuning and pm.max_children works out the number from the server you have.
For a runaway process that is genuinely stuck, restart its service instead of killing the process by hand. A service restart is clean: it closes connections properly, flushes logs, and starts fresh. A kill -9 leaves half-written files and unclosed connections behind, and a database killed that way may need recovery on the next start. Use the service command:
sudo systemctl restart php8.3-fpm
sudo systemctl restart mysql
Only kill a single process by PID when the service restart does not work, and only after you have confirmed the PID belongs to the process you think it does. Check with ps -p <PID> -o pid,cmd before you send any signal. Killing the wrong PID on a production server is the kind of mistake that takes the site down for an hour.
After the fix, watch the server for a day. Run free -h at busy times and check that available stays above 300 MB. If the same process grows back to the same size, the setting did not take or the leak is in the code, and the next step is the application logs, not the server. A WooCommerce shop that grows workers with every order may need its plugin list reviewed, and this guide to how much RAM and CPU a WooCommerce store needs on a VPS covers that case. The goal is not a server with free memory sitting idle, it is a server where the memory goes to the work that matters, and every process earns the RAM it holds.