Running a Linux VPS (Virtual Private Server) can feel great—until something goes wrong. One common problem I’ve run into is the “Out of Memory” error. It usually shows up when your server runs out of RAM and can’t handle any more tasks. Things slow down, crash, or freeze. If you’ve seen weird behavior like that, your VPS might be running out of memory too.
In this post, I’ll explain what’s going on, how to check it, and how to fix it. Don’t worry—I’ll keep it simple. I’ve dealt with this issue a few times, and I’ll show you what actually worked for me.
What Does “Out of Memory” Mean?
Let’s keep it easy. Your VPS has something called RAM (Random Access Memory). RAM is the short-term memory your server uses to run programs. The more things your server is doing, the more RAM it needs.
If your server tries to use more RAM than it has, Linux says, “Nope, not today,” and starts killing programs to save itself. That’s the Out of Memory (OOM) error. It’s kind of like a computer version of a nap—only it’s not relaxing.
How It Shows Up
Sometimes you’ll see a message like this:
Out of memory: Kill process 1234 (mysqld) score 500 or sacrifice child
Or maybe things just start crashing for no reason.
I’ve had MySQL vanish, PHP stop working, or even the whole server freeze. That’s usually when I realize: “Oops, I pushed it too far.”
What Causes It?
There are a few common reasons for this problem:
- Too many apps: Running too many services (like MySQL, Nginx, Redis, etc.)
- Heavy traffic: Lots of people visiting your site at once
- Memory leaks: A program keeps using more and more RAM and doesn’t let go
- Big scripts: Running huge tasks like backups, imports, or video processing
Once I ran a backup script and forgot to limit it. My little 1GB RAM VPS cried for help and crashed.
Step 1: Check What’s Eating the Memory
Before fixing anything, let’s take a look around.
You can run this command to see what’s using the most RAM:
top
Or, for a cleaner view:
htop
(If htop
isn’t installed, try sudo apt install htop
or sudo yum install htop
.)
Look for lines showing programs using a lot of memory. You might see names like:
mysqld
(MySQL)php-fpm
node
java
If one of them looks way too big, you’ve found your greedy memory monster.
Step 2: Free Up Some Space
Sometimes, the quickest fix is to restart a big service. For example:
sudo systemctl restart mysql
Or:
sudo systemctl restart php7.4-fpm
This doesn’t solve the root problem, but it can buy you some time.
You can also remove unused stuff:
sudo apt autoremove
sudo apt clean
Less clutter = more breathing room.
Step 3: Add Swap Space
Swap is like fake RAM that lives on your hard drive. It’s slower than real memory, but better than crashing. It helps when your RAM fills up.
I always add swap to small VPS servers. Here’s how you can do it:
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Then make it permanent:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
To check it:
free -h
You’ll now see a “Swap” line with something like 1.0G
.
Tips:
- On servers with low disk space, don’t go overboard with swap.
- 1GB to 2GB is usually enough for basic use.
Swap has saved me many times, especially during traffic spikes.
Step 4: Stop Unnecessary Services
Sometimes, your server is running stuff you don’t even use.
Run:
ps aux --sort=-%mem | head
This shows the top memory-hungry processes.
Then check if you actually need them. If not, disable them:
sudo systemctl stop some-service
sudo systemctl disable some-service
Some things I’ve disabled on tiny servers:
- Postfix (mail server)
- Dovecot
- Apache (if I only use Nginx)
- Unused backups or cron tasks
Step 5: Set Limits on Programs
You can also make sure big apps don’t hog all the RAM.
For PHP (using PHP-FPM), edit this file:
/etc/php/7.4/fpm/pool.d/www.conf
Look for:
pm.max_children
Lower this number to reduce how many PHP processes run at once.
For MySQL, edit:
/etc/mysql/my.cnf
Look for settings like:
innodb_buffer_pool_size
max_connections
Lower them to save memory. You may need to experiment a bit here. I usually start low, then go up slowly.
Three Helpful Commands to Keep Handy
Here are a few commands I use often when checking for memory issues:
free -m
– shows memory and swap in MBtop
orhtop
– real-time process listdmesg | grep -i oom
– shows if the kernel killed any apps
Try running them when your server feels sluggish.
Three Ideas to Prevent OOM Errors
To stop the problem before it happens again:
- Add swap space – even just 1GB can help a lot
- Monitor usage – use tools like
htop
,glances
, or basic scripts - Optimize apps – don’t let one service use all your RAM
If your site grows, your server needs to grow too. It’s like outgrowing your favorite hoodie—eventually, it just doesn’t fit.
Three Signs Your Server Is Running Low on Memory
Keep an eye out for these:
- Commands feel slow or hang
- Web pages stop loading
- You see OOM messages in the logs
I once had a site that slowed to a crawl every time I ran a backup. Turned out I had no swap and MySQL was taking a nap.
Bonus Pun Corner
Because Linux needs a sense of humor too:
- Why did the VPS go to therapy? It couldn’t process its memory problems.
- I told my server to “take a break,” and it took down the whole site.
- Swap is like borrowing your friend’s couch—helpful, but not as comfy as your own bed.
Wrapping Up
Out of Memory errors can be scary at first. But with a few simple tools and tweaks, you can keep your server calm and happy.
Here’s what I suggest:
- Check what’s using memory
- Add swap if you haven’t already
- Keep only what you need running
- Set limits so nothing goes wild
Don’t wait until things crash. A little prep now saves a big headache later.
Got a memory horror story? Or a swap trick that saved the day? I’d love to hear it. What did your server say when it ran out of memory—”I can’t think straight!”?
Let’s keep our servers running smooth and stress-free.