Find What's Using All Memory on a Small VPS
In this article
This guide shows you how to pinpoint which processes are consuming memory on a small VPS, so you can decide whether to kill a runaway process, adjust an application, or upgrade your plan. You'll use standard Linux commands that work on most distributions (Ubuntu, Debian, CentOS, etc.). No prior experience is needed, but you should have SSH access to your server.
What You'll Need
- SSH access to your VPS (username and password or SSH key).
- Basic familiarity with the command line (typing commands and reading output).
- Root or sudo privileges (to view all processes and free memory).
Step-by-Step
- Log in to your VPS. Open a terminal (on Windows, use PowerShell or PuTTY) and run:
ssh user@your-server-ip. Replaceuserwith your username andyour-server-ipwith your server's IP address. - Check overall memory usage. Run
free -h. This shows total, used, and available memory in human-readable format (e.g., 1.9G). Note the "available" column – it's the best indicator of how much memory is actually free for new processes. - List processes sorted by memory usage. Run
ps aux --sort=-%mem. This lists all running processes, sorted from highest to lowest memory percentage. The%MEMcolumn shows the percentage of total RAM each process uses. TheRSScolumn shows the actual memory in kilobytes (divide by 1024 to get MB). - Identify the top memory consumers. Look at the first 10-15 lines. Focus on processes with high
%MEMorRSS. Common culprits on a small VPS include: MySQL/MariaDB, Apache/Nginx, PHP-FPM, Node.js, Java, or a runaway script like a WordPress cron job. - Get more detail on a specific process. Note the PID (second column). Run
ps -p PID -o pid,user,cmd,%mem,rssto see the full command line and exact memory usage. For example:ps -p 1234 -o pid,user,cmd,%mem,rss. - Check memory usage in real time. Run
top(orhtopif installed). This shows a live-updating list of processes. PressMto sort by memory usage. Pressqto exit. - Investigate the process. Use
systemctl status PID(if it's a systemd service) orls -l /proc/PID/cwdto see the working directory. This helps you identify what application the process belongs to. - Decide what to do. If it's a service you don't need, stop it permanently:
sudo systemctl disable --now service-name. If it's a temporary process, you can kill it withsudo kill -9 PID(use with caution). If it's a legitimate service, consider adjusting its configuration to use less memory (e.g., reduce PHP-FPM workers, MySQL buffer pool size). - Monitor after action. Run
free -hagain to confirm memory is freed. If memory usage climbs again, the issue may be a memory leak or a service that needs tuning.
Common Mistakes
- Misreading the
freeoutput. On Linux, "used" memory includes cache/buffers. The "available" column is what matters; it accounts for reclaimable cache. Don't panic if "used" is high – it's normal. - Killing a critical system process. Always verify what the process is before killing it. Killing
sshdorsystemdcan lock you out or crash the server. - Ignoring swap usage. If your server is using swap heavily, it's a sign of memory pressure. Check with
free -h; if the swap column is high, you need to free up RAM or add more. - Forgetting about cached memory. Linux uses free RAM for caching files. This is not a problem; it's a feature. Your "available" memory will be lower than "free" but that's fine.
Tips
- Use
htopif available. It's more user-friendly thantopand allows you to kill processes with F9. Install it withsudo apt install htop(Debian/Ubuntu) orsudo yum install htop(CentOS). - Check for memory leaks. If a process's memory usage grows over time, it may be leaking. Use
watch -n 5 'ps -p PID -o rss'to monitor it. - Look at logs. Check
/var/log/syslogor/var/log/messagesfor OOM (Out of Memory) killer messages. The OOM killer kills processes when memory is exhausted; logs will show which process was killed. - Consider using a process manager. Tools like PM2 (for Node.js) or Supervisor can automatically restart services that crash due to memory issues.
Troubleshooting
- If
freeshows low available memory but no process is using much: This could be due to kernel memory or slab cache. Runcat /proc/meminfoand look atSlabandSReclaimable. If they're huge, it's often due to a filesystem cache. Tryecho 3 > /proc/sys/vm/drop_caches(as root) to drop caches temporarily. - If you can't log in because the server is unresponsive: Your VPS provider likely has a web console (e.g., VNC) that you can use to log in. From there, run the commands above. If the server is completely frozen, you may need to reboot from the provider's panel.
- If a process you killed comes back: It's likely a service managed by systemd or a supervisor. Disable it with
sudo systemctl disable service-nameor stop the supervisor. - If memory usage is still high after killing processes: Check for zombie processes (state Z in
ps). These are processes that have finished but haven't been cleaned up by their parent. You can't kill them, but they don't use memory. If they accumulate, it's a bug in the parent process.
Frequently Asked Questions
What is the difference between used and available memory in free -h?
Used memory includes memory used by processes and also memory used for caching and buffers. Available memory is the amount that can be allocated to new processes without swapping. It's the more reliable number to watch.
Why does my VPS show 1.9G total but only 1.5G available?
The kernel reserves some memory for its own use, and some memory is used for hardware reservations. This is normal; you can't use 100% of the RAM.
How can I see memory usage per user?
Run ps aux --sort=-%mem | awk '{print $1}' | sort | uniq -c to count processes per user, or use top and press u to filter by user.
What is the OOM killer and how do I know if it's active?
The OOM killer is a kernel feature that kills processes when memory is exhausted. Check dmesg | grep -i oom or journalctl -k | grep -i oom to see if it has been triggered.
Can I increase swap to fix memory issues?
Yes, adding swap can help, but it's not a substitute for enough RAM. Swap is much slower than RAM and can cause high load. It's better to reduce memory usage or upgrade your VPS.
Is it safe to kill a process with kill -9?
It's safe for user processes, but it doesn't allow the process to clean up. It can cause data loss or corruption for databases. Prefer kill (SIGTERM) first, and only use -9 if the process doesn't respond.