Why VPS out of memory happens and how to resolve it: Practical Guide
Learn why VPS out of memory errors occur and how to diagnose, resolve, and prevent them with practical troubleshooting steps for hosting professionals.

On this page
TL;DR — Key takeaways
- VPS out of memory errors occur when applications consume more RAM than allocated, triggering the Linux OOM killer to terminate processes automatically.
- Use free -h, top, and dmesg to identify memory usage patterns, runaway processes, and OOM kill events in real time.
- Resolve memory issues by stopping unnecessary services, optimizing application configurations, adding swap space, or upgrading to a larger VPS plan.
- Prevent future OOM events by monitoring memory usage with alerts, implementing resource limits with cgroups, and regularly reviewing application memory footprints.
A VPS out of memory error stops applications, kills processes unexpectedly, and disrupts service availability. When your virtual private server exhausts its allocated RAM, the Linux kernel's Out of Memory (OOM) killer terminates processes to free resources, often taking down critical services without warning.
This guide explains why VPS memory exhaustion happens, how to diagnose the root cause, and how to resolve and prevent OOM conditions using practical troubleshooting steps. Whether you manage a single VPS or support hosting customers, these methods help restore stability and improve resource planning.
What causes VPS out of memory conditions
VPS out of memory errors occur when running processes request more RAM than the system can provide. Unlike physical servers with swap-backed emergency buffers, many VPS configurations run with limited or no swap space, making memory exhaustion immediate and severe.
Common causes include application memory leaks where programs fail to release allocated memory, traffic spikes that spawn too many concurrent worker processes, inefficient database queries that cache large result sets in RAM, and resource-intensive background tasks running simultaneously with production workloads.
Shared hosting migrations to VPS often trigger OOM conditions because users underestimate memory requirements. A WordPress site that ran smoothly on shared hosting may consume 512 MB to 1 GB of RAM when all plugins, caching layers, and PHP workers run independently on a VPS.
How to identify memory exhaustion on your VPS
Start by checking current memory usage with the free command. Run free -h to display RAM and swap usage in human-readable format. Look for low available memory under the 'available' column—values below 100 MB indicate memory pressure.
Use top or htop to identify which processes consume the most RAM. Press Shift+M in top to sort by memory usage. Note processes with high RES (resident memory) values, especially if they show steady growth over time, indicating a potential memory leak.
Check system logs for OOM killer activity using dmesg | grep -i 'killed process'. The kernel logs show which processes were terminated due to memory exhaustion and when the kills occurred. Repeated OOM kills of the same process indicate a configuration or application issue rather than a temporary spike.
- free -h: View total, used, and available RAM
- top or htop: Identify high-memory processes in real time
- dmesg | grep -i oom: Review OOM killer logs
- vmstat 1: Monitor memory paging and swapping activity
- cat /proc/meminfo: Access detailed memory statistics
Immediate steps to resolve VPS memory issues
First, identify and stop non-critical services to free RAM immediately. Use systemctl list-units --type=service --state=running to see active services, then stop unnecessary ones with systemctl stop service_name. Common candidates include development tools, unused database servers, or redundant monitoring agents.
Restart services consuming excessive memory. For web servers, use systemctl restart apache2 or systemctl restart nginx. For PHP-FPM, use systemctl restart php-fpm or the version-specific command like systemctl restart php8.1-fpm. For MySQL or MariaDB, use systemctl restart mysql or systemctl restart mariadb. Always verify the service restarts successfully with systemctl status service_name.
If OOM conditions persist, add temporary swap space to prevent immediate crashes while you implement permanent fixes. Create a 2 GB swap file with sudo fallocate -l 2G /swapfile, set permissions with sudo chmod 600 /swapfile, format it with sudo mkswap /swapfile, and enable it with sudo swapon /swapfile. This provides emergency headroom but is not a long-term solution since swap on VPS storage degrades performance.
- Stop unnecessary services to free RAM immediately
- Restart memory-leaking processes to reclaim leaked memory
- Add temporary swap space for emergency stability (2-4 GB typical)
- Clear page cache if safe: sync && echo 1 > /proc/sys/vm/drop_caches
- Kill specific high-memory processes as last resort: kill -9 PID
Optimize application and service configurations
Web server worker processes often consume excessive memory. For Apache, reduce MaxRequestWorkers in /etc/apache2/mods-available/mpm_prefork.conf or mpm_event.conf. Start with 25-50 workers for a 2 GB VPS. For Nginx, reduce worker_processes to match CPU cores and worker_connections to 512-1024 in /etc/nginx/nginx.conf.
PHP-FPM pools require tuning for VPS environments. Edit pool configuration files in /etc/php/8.1/fpm/pool.d/www.conf (adjust version as needed). Set pm.max_children based on available RAM divided by average PHP process size (typically 50-80 MB per process). For a 2 GB VPS, use 15-20 max children. Set pm.start_servers to 25% of max_children and pm.min_spare_servers to 10% of max_children.
Database servers benefit from conservative memory limits. For MySQL or MariaDB, edit /etc/mysql/my.cnf or /etc/my.cnf. Set innodb_buffer_pool_size to 50-60% of available RAM (e.g., 1 GB for a 2 GB VPS after accounting for OS and web server overhead). Reduce max_connections to 50-100 for small VPS instances. Restart the database service after changes and monitor performance.
- Apache: Reduce MaxRequestWorkers to 25-50 for small VPS instances
- Nginx: Limit worker_connections to 512-1024
- PHP-FPM: Set pm.max_children based on RAM / avg_process_memory
- MySQL: Limit innodb_buffer_pool_size to 50-60% of RAM
- Redis/Memcached: Set maxmemory limits with eviction policies
Long-term prevention and monitoring strategies
Implement proactive memory monitoring with alerting. Set up threshold alerts when available memory drops below 20% or 200 MB. Use built-in monitoring tools from your hosting provider, or configure open-source solutions like Netdata or Prometheus with Alertmanager for custom alerts.
Use cgroups to enforce resource limits per application or user. This prevents single processes from consuming all available RAM. Create a memory limit with systemctl set-property service_name.service MemoryLimit=512M to restrict a service to 512 MB. For containerized workloads, set memory limits in Docker Compose or Kubernetes resource definitions.
Review and audit installed applications regularly. Remove unused software, disable unnecessary plugins, and consolidate redundant services. A typical production VPS should run only essential services: web server, database, application runtime, and monitoring agent.
Plan capacity upgrades based on growth trends. If your VPS consistently uses over 80% of allocated RAM during normal operation, upgrade to the next tier before hitting OOM conditions. Most hosting providers allow seamless upgrades with minimal downtime. Schedule upgrades during maintenance windows and verify all services restart correctly after the upgrade completes.
When to upgrade versus optimize
Optimization makes sense when memory spikes result from misconfigurations, unnecessary services, or inefficient application settings. If your VPS runs below 70% memory usage most of the time but spikes during backups or traffic bursts, optimize configurations and add swap space for headroom.
Upgrade when baseline memory usage exceeds 80% after optimization efforts. If all services are properly tuned, unnecessary software removed, and the VPS still runs memory-constrained during normal operation, the workload legitimately requires more RAM. Calculate requirements by summing average memory usage for each service plus 20% overhead for OS operations and temporary spikes.
Consider horizontal scaling for high-traffic applications. Instead of upgrading to an 8 GB or 16 GB VPS, distribute workload across multiple smaller instances with a load balancer. This approach improves fault tolerance and allows incremental scaling as traffic grows. Use managed database services to offload memory-intensive database operations from application servers.
Quick troubleshooting checklist
- Check current memory usage with free -h and identify available RAM
- Review top or htop output sorted by memory to find high-consumption processes
- Search system logs with dmesg | grep -i oom for OOM killer events
- Stop non-critical services to free RAM immediately
- Restart services showing memory leaks or high usage
- Add temporary swap space (2-4 GB) for emergency stability
- Reduce web server worker limits in Apache or Nginx configuration
- Optimize PHP-FPM pool settings based on available RAM
- Tune database memory limits (innodb_buffer_pool_size for MySQL)
- Set up memory monitoring with alerts at 80% usage threshold
- Implement cgroups or systemd resource limits for critical services
- Remove unused software and disable unnecessary services
- Schedule VPS upgrade if baseline usage exceeds 80% after optimization
FAQ
What does VPS out of memory mean?
VPS out of memory means the virtual private server has exhausted its allocated RAM, causing the Linux kernel to terminate processes automatically through the OOM (Out of Memory) killer to free resources. This results in service disruptions, application crashes, and potential data loss if critical processes are killed.
How do I check if my VPS is running out of memory?
Use the command free -h to check current memory usage and available RAM. Run top or htop to identify high-memory processes sorted by memory consumption. Check system logs with dmesg | grep -i 'killed process' to see if the OOM killer has terminated any processes due to memory exhaustion.
Can I add more RAM to my VPS without upgrading?
You cannot physically add RAM to a VPS, but you can add swap space as temporary relief. Create swap with fallocate -l 2G /swapfile, format with mkswap /swapfile, and enable with swapon /swapfile. Swap provides emergency headroom but degrades performance. For permanent solutions, optimize services or upgrade your VPS plan.
How much memory should I allocate to MySQL on a VPS?
Allocate 50-60% of available RAM to the MySQL innodb_buffer_pool_size setting after accounting for web server and OS overhead. For a 2 GB VPS, allocate approximately 1 GB to MySQL. For a 4 GB VPS, allocate 2-2.5 GB. Always leave at least 500 MB free for the operating system and other services.
What is the OOM killer and why does it terminate my processes?
The OOM (Out of Memory) killer is a Linux kernel mechanism that terminates processes when the system runs out of memory to prevent complete system failure. It selects processes based on memory usage and priority, killing those consuming the most RAM while trying to preserve critical system services. OOM killer activation indicates the VPS needs optimization or more memory.
Should I use swap space on my VPS?
Use swap space as a safety buffer, not a primary memory solution. Allocate 1-2 GB of swap for small VPS instances (1-2 GB RAM) or 2-4 GB for larger instances. Swap prevents immediate crashes during temporary memory spikes but causes severe performance degradation when actively used. If your VPS regularly swaps, upgrade RAM instead of relying on swap.
Related articles
- Hosting OperationsSelf-Hosted App Deployment Fails? Check DNS, SSL, Reverse Proxy, and Logs FirstTroubleshoot failed self-hosted app deployments by checking DNS, SSL, reverse proxy routing, container status, logs, and ports.
- Hosting OperationsSelf-Hosted PaaS on a VPS: What to Check Before Installing Coolify, Dokploy, or CapRoverA hosting support checklist for preparing a VPS before installing self-hosted PaaS tools like Coolify, Dokploy, or CapRover.
- Hosting OperationsLinux Server Security Lessons from the Arch Linux Malware Package IncidentPractical Linux server security checklist for VPS admins after package malware concerns, with safe checks, rollback steps, and support guidance.
- Hosting OperationsAWS Lightsail Hong Kong VPS Latency: Practical Hosting Guide for IndonesiaLearn how to test AWS Lightsail Hong Kong VPS latency, compare regions, migrate safely, and troubleshoot hosting performance.
- Hosting OperationsCloudflare Tomorrow Watchlist: A Practical Hosting Operations GuidePractical Cloudflare troubleshooting checklist for DNS, SSL, caching, WAF, origin health, safe testing, and rollback planning.
- Hosting OperationsNetwork Safety Checklist for AI Agent Skills in Hosting OperationsAudit AI agent skills safely with network checks, secret protection, sandbox testing, rollback steps, and hosting support troubleshooting guidance.