Server Out of Memory: Why Programs Get Killed
A website that works for days and then stops, a database that quits with no warning, a server that becomes slow and then recovers: these can all be signs of the same cause. The server ran out of memory, and Linux ended a program to survive. This guide shows how to confirm it and what to do.
What happens
Memory, or RAM, is the fast workspace of a server. Every program uses some. When all of it is used, Linux has a safety tool called the out of memory killer. It picks a program, often a large one such as the database, and stops it. To you, it looks like a random crash.
How to confirm
Log in over SSH and read the system log for the words “out of memory” or “killed process”. The message names the program that was stopped and the time. Also run the free command to see how much memory is in use right now, and a tool like top to see which program uses the most.
Common causes
- A site with too many visitors for the size of the server.
- A database that is not tuned and takes too much memory.
- Too many web server workers started at once.
- A plugin or script with a memory leak, which grows until it fills everything.
- Many services on one small server, such as web, mail and database together.
- Backups or imports that run at the same time as busy hours.
What to do
- Find the heaviest program. Fix or limit it first.
- Lower the workers. Web servers and PHP can be set to start fewer workers at once.
- Tune or restart the database in a planned way.
- Move heavy jobs to quiet hours. See our cron job video guide for the idea of scheduled tasks.
- Check the website. A slow plugin can use too much. See speed up a slow WordPress site.
- Add swap as a short-term cushion. Swap uses disk as slow memory. It prevents sudden kills but it is not a fix for lack of RAM.
- Move to a larger server when your usage has truly grown.
How to know your size is right
Read server RAM, CPU and storage to learn how to judge needs. If you outgrow cloud hosting, see move from cloud hosting to a VPS. If a VPS becomes tight, see VPS or dedicated server.
Watch it over time
A one-time check does not show a slow leak. Use simple server monitoring to see memory use through the day and spot the moment it climbs.
After a kill
Check that your database and website came back. Look at the data for damage. If it did not restart, see restart a Linux server safely and the website down checklist.
A short example
Imagine a small server that runs a website, a database and mail together. At the end of each month a report script runs and the database stops at the same hour. The log shows the kill message with the database name. The fix is to run the report at night with fewer workers, lower the web server workers and add a small swap area. Memory use stays under the limit, and the database stays up. Your own case will differ, but the method is the same: read the log, find the time pattern, then reduce the load at that time.
FAQ
Is swap bad?
No. A little is fine as a safety net. Heavy swap use means the server is too small for the job.
Will a restart fix it?
It clears memory for a while. The cause remains and the problem returns.
Can I ask for help?
Yes. Open a support ticket with the log lines and the time they happened.