VPS Tutorials
VPS Out of Memory? OOM Problem Solutions
Is your VPS running out of memory and triggering the OOM Killer? This article explains in detail how to diagnose OOM problems, add Swap, and optimize MySQL and PHP-FPM memory configurations to completely solve the problem of processes being killed due to insufficient memory on a Linux server.

The server suddenly slows down, processes get killed for no apparent reason, MySQL goes down without warning... If you've run into these situations, there's a good chance that insufficient memory has triggered the OOM Killer. This article teaches you how to diagnose and solve VPS out-of-memory problems.
OOM is short for Out of Memory. When the Linux system runs out of memory, the kernel activates the OOM Killer mechanism, selectively killing some processes to free up memory and ensure the system doesn't die completely.
The processes that get killed are usually the ones using a lot of memory, such as MySQL, Java applications, Node.js services, and so on. That's why your database or website service suddenly goes down.
# 搜索 OOM 相关日志
dmesg | grep -i "out of memory"
dmesg | grep -i "oom"
# 或者查看 syslog
grep -i "oom" /var/log/syslog
grep -i "oom" /var/log/messages
If you see output similar to this, it means OOM has indeed occurred:
Out of memory: Killed process 1234 (mysqld) total-vm:2048000kB, anon-rss:1024000kB
# 查看内存概况
free -h
# 输出示例:
# total used free shared buff/cache available
# Mem: 1.9Gi 1.5Gi 128Mi 64Mi 300Mi 200Mi
# Swap: 0B 0B 0B
Pay close attention to the available column—this is the memory that is truly available. If this value is very small (for example, below 100MB), it means memory really is tight.
# 按内存占用排序显示进程
ps aux --sort=-%mem | head -20
# 或者用 top,按 M 键按内存排序
top
If your VPS has no Swap configured, it is strongly recommended that you add it. Swap can provide a buffer when memory runs low; although it is slower, it's better than having processes killed.
# 检查是否有 swap
swapon --show
# 如果没有,创建 2G 的 swap 文件
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# 设置开机自动挂载
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Recommended Swap size:
- 1GB memory: set Swap to 1-2GB
- 2GB memory: set Swap to 2GB
- 4GB or more: setting Swap to 2-4GB is enough
MySQL is a major memory consumer, and VPSs with small amounts of memory must be tuned. Edit the configuration file:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Recommended configuration for a 1GB memory VPS:
[mysqld]
# 关键参数
innodb_buffer_pool_size = 128M
innodb_log_buffer_size = 8M
key_buffer_size = 16M
# 连接相关
max_connections = 50
thread_cache_size = 8
# 临时表
tmp_table_size = 16M
max_heap_table_size = 16M
# 查询缓存(MySQL 8.0 已移除)
query_cache_size = 0
query_cache_type = 0
Restart MySQL after making changes:
sudo systemctl restart mysql
If you run WordPress or other PHP applications, the number of PHP-FPM processes also needs to be controlled.
Edit the PHP-FPM configuration:
# 路径可能不同,根据你的 PHP 版本调整
sudo nano /etc/php/8.1/fpm/pool.d/www.conf
Recommended configuration for 1GB memory:
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500
Using systemd, you can limit the maximum memory usage of a service:
# 编辑服务配置
sudo systemctl edit mysql
Add the memory limit:
[Service]
MemoryMax=512M
MemoryHigh=400M
This way MySQL can use at most 512MB of memory, and when it exceeds 400MB the system will try to reclaim memory.
You can lower the priority of important processes being killed:
# 查看进程的 oom_score(分数越高越容易被杀)
cat /proc/$(pidof mysqld)/oom_score
# 调整 oom_score_adj(范围 -1000 到 1000,-1000 表示永不被杀)
echo -500 > /proc/$(pidof mysqld)/oom_score_adj
To make it permanent, add it to the systemd service file:
[Service]
OOMScoreAdjust=-500
Write a simple memory monitoring script that sends an alert when memory is too low:
#!/bin/bash
# /usr/local/bin/memory-monitor.sh
THRESHOLD=100 # 可用内存低于 100MB 时告警
available=$(free -m | awk '/Mem:/ {print $7}')
if [ $available -lt $THRESHOLD ]; then
echo "警告:可用内存仅剩 ${available}MB" | mail -s "VPS 内存告警" [email protected]
fi
Run it periodically with cron:
# 每 5 分钟检查一次
*/5 * * * * /usr/local/bin/memory-monitor.sh
If you have already:
- Added Swap
- Optimized the memory configuration of each service
- Disabled unnecessary services
but memory is still frequently insufficient, then it's time to consider upgrading your VPS configuration. Generally speaking:
- A single small site: 1GB of memory is enough
- WordPress + a few small services: 2GB is more comfortable
- Multiple sites or Java applications: start at 4GB
The approach to handling OOM problems:
- Confirm first - Check the logs to confirm whether OOM has actually occurred
- Find the cause - Identify the processes using a lot of memory
- Add a buffer - If there's no Swap, add it first
- Optimize - Adjust the configuration of services such as MySQL and PHP-FPM
- Set limits - If necessary, use systemd to limit process memory
- Set up monitoring - Configure alerts so problems are found and handled early
Running services on a small-memory VPS requires careful budgeting, but as long as it's configured properly, 1GB of memory is completely fine for running a personal blog or small project.