Linux VPS Running Out of Memory: Swap, OOM Killer, and What to Do
Linux VPS Running Out of Memory: Swap, OOM Killer
When a Linux VPS runs out of memory, the problem rarely announces itself clearly. Services may start responding more slowly, background jobs can lag behind schedule, and some processes disappear without any obvious error message. In many cases the system still accepts connections and reports normal CPU usage, which makes the situation especially confusing.
When a Linux VPS starts running out of memory, the process is rarely sudden. The kernel begins compensating long before users notice that anything is wrong. It reclaims cache, shifts inactive pages to swap, and keeps the system running in a degraded but stable state. By the time a service restarts or a process gets killed, the underlying pressure has usually been building for hours or even days.
This article explains how memory pressure develops on a Linux VPS, how swap and the OOM killer fit into that process, and how to reason about the problem without relying on misleading surface metrics.
How Linux Handles Memory Pressure
Linux does not treat unused RAM as a goal. Available memory is deliberately used for page cache and buffers because cached data improves performance and can be discarded quickly when applications need memory.
Because of this, low free memory by itself is not a warning sign. A VPS can operate normally with almost no free RAM as long as the kernel can still reclaim memory when needed. This behavior is expected and intentional.
What matters is how much memory can be allocated without heavy reclaim or swapping. That is why the available metric is far more useful than free when diagnosing memory-related issues.
As pressure increases, Linux follows a predictable sequence. It starts by reclaiming cache. If that is not enough, inactive pages are moved to swap. Only when these mechanisms fail to satisfy allocation requests does the kernel escalate further. Each stage changes system behavior in a different way, which is why understanding this order is critical.
How to Check Real Memory Usage on a Linux VPS
Memory problems cannot be understood from a single snapshot. A momentary view of system state rarely explains why performance degrades under sustained load.
Commands like free -h are useful, but only if you read them correctly. Shrinking available memory combined with growing swap usage usually indicates ongoing pressure rather than a short-lived spike. Looking at free memory alone hides this pattern.
Trend-oriented tools provide much more insight. If swap-in and swap-out activity continues over time, the kernel is constantly reclaiming memory instead of running workloads. Occasional bursts are normal. Continuous activity is not.
When the picture is still unclear, /proc/meminfo shows how memory is actually allocated. It allows you to distinguish reclaimable page cache from anonymous memory that cannot be freed easily. That distinction often determines whether the system can recover or is approaching OOM conditions.
When Swap Helps and When It Becomes a Problem
Swap exists to absorb temporary memory spikes. It is a safety mechanism, not an extension of physical RAM.
During short bursts of activity, swap allows the system to remain responsive by moving inactive pages out of memory. In this role, swap is functioning exactly as intended.
The situation changes when swap usage becomes persistent. In most general-purpose VPS workloads, constant swapping means the kernel is compensating for a real memory shortage rather than smoothing brief fluctuations. Performance degrades gradually, which makes the problem easy to miss at first.
Tuning swappiness can influence when swapping starts, but it does not fix insufficient memory or uncontrolled growth. If swap is required just to keep the system stable, the workload no longer fits into available RAM.
Why the OOM Killer Terminates Processes
When the kernel can no longer reclaim enough memory and swap cannot satisfy allocation requests fast enough, it activates the OOM killer. This is not a crash. It is a last-resort survival mechanism.
The kernel selects a victim based on internal scoring. Memory usage, runtime, and priority adjustments all influence the decision. Because of this, the terminated process is often not the one administrators expect.
OOM events are usually recorded in system logs or visible through dmesg. Once the system reaches this point, the goal is no longer to prevent a single OOM event. The real task is to understand what pushed the system into a state where killing a process became unavoidable.
Common Causes of Memory Exhaustion on a VPS
Most real-world memory failures follow familiar patterns, but they rarely appear in isolation. In practice, several small decisions accumulate and push the system into sustained memory pressure long before anything actually fails.
Memory leaks in long-running services are a typical example. The footprint grows slowly. The kernel compensates by reclaiming cache and using swap, and the VPS continues to operate. The failure only looks sudden because the pressure was invisible until reclaim and swapping stopped being enough.
Containerized workloads introduce a different class of risk. Containers without explicit memory limits draw from the same RAM pool, and a single misbehaving service can shift the entire system into constant reclaim activity.
Configuration choices matter just as much. Oversized worker pools, aggressive caching, or poorly tuned runtimes raise the baseline memory usage. Once that baseline becomes too high, even minor load increases can push the system into OOM conditions.
How to Fix and Prevent Memory Issues
Start by identifying what grows over time. If a process steadily increases its memory usage, restarting it may temporarily restore stability, but it does not change the underlying behavior.
Limits are what turn uncontrolled pressure into predictable failure modes. Explicit memory limits for containers and background jobs prevent one component from consuming resources required by the rest of the system. This does not eliminate memory pressure. It keeps it contained.
Swap should be treated as a buffer, not a solution. It absorbs short spikes. When it is used continuously, it only masks sizing problems instead of fixing them.
Monitoring closes the loop. Without visibility into trends, memory exhaustion will always be detected too late, after the kernel has already begun compensating.
When the Problem Is Not Actually Memory
Memory pressure often interacts with other bottlenecks, which makes diagnostics misleading. In practice, this situation appears when the system reacts to blocked tasks rather than to real RAM exhaustion.
High load averages do not always mean CPU saturation. Sometimes the processor is mostly idle, while the load continues to grow. This happens because tasks are waiting on resources instead of executing, and the scheduler still counts them as runnable.
At this point, memory becomes a secondary signal. The kernel reallocates resources in response to stalled workloads, cache behavior changes, and RAM usage starts to look suspicious. It is tempting to tune memory or swap, but those changes rarely help while the real constraint remains unresolved.
When Disk Behavior Amplifies Memory Symptoms
Disk-related problems distort diagnostics through backpressure. Slow storage, excessive logging, or rapidly growing container layers delay I/O completion and force processes to wait longer than expected.
When I/O stalls, queues begin to grow. Data stays resident in memory. Buffers are held longer, and pending operations accumulate. None of this is caused by RAM itself.
In this situation, memory reacts to latency elsewhere in the system. Pressure increases as a side effect, not as a primary failure. Treating the issue as a memory problem shifts attention away from the actual bottleneck and usually leads to ineffective tuning.
If containers are involved, this scenario is covered in detail here: Docker Disk Space Issues on VPS.
Summary
Running out of memory on a Linux VPS is rarely caused by a single mistake. Swap, page cache, and the OOM killer are mechanisms designed to keep the system alive under pressure, not to prevent pressure entirely.
Once you understand how these components interact and observe behavior over time, memory-related failures become predictable. They stop being mysterious and start being manageable.