Skip to content

20. Memory Management

In a DevOps environment, memory (RAM) is often the tightest bottleneck. When a server runs out of memory, applications crash, databases corrupt, and the system can become entirely unresponsive. Understanding how the Linux Kernel allocates and protects memory is critical.

Linux doesn’t give applications direct access to the physical RAM hardware. Instead, the Kernel provides Virtual Memory—an illusion of a vast, contiguous block of memory. The Kernel maps these virtual addresses to physical RAM using “Pages” (typically 4KB chunks). This prevents one application from overwriting the memory of another.

  • RAM (Random Access Memory): The blazing-fast physical memory chips on the motherboard.
  • Swap: A dedicated partition or file on the much slower hard drive. If the physical RAM is completely full, the Kernel moves inactive pages from RAM into Swap to prevent the system from crashing.
    • Note: While Swap prevents crashes, heavy reliance on it (called “thrashing”) will slow the system to an absolute crawl.

Linux purposefully tries to use all available RAM to speed up the system.

  • Page Cache: When you read a file from the slow hard drive, Linux stores a copy of it in the Page Cache (RAM). The next time you read it, it loads instantly.
  • Buffers: Similar to cache, but used for temporary metadata (like directory structures or disk block locations).
  • Don’t panic if your RAM looks “full”! The Kernel will instantly drop the Page Cache if an application actually needs the memory.

If both physical RAM and Swap fill up completely, the Kernel enters an Out-Of-Memory (OOM) state.

To save the system from completely freezing, the Kernel invokes the OOM Killer. This is a ruthless routine that scans all running processes, scores them based on memory usage and priority, and sends a SIGKILL to the highest-scoring process to free up RAM.

In the Docker/Kubernetes world, the OOM Killer is the most common reason your containers unexpectedly die (often accompanied by an OOMKilled status).

The quickest way to check current memory usage.

Terminal window
# View memory in Human-readable format (MB/GB)
free -h

Pay close attention to the available column, NOT the free column. The available column includes the memory currently used by the Page Cache that can be reclaimed if needed.

Provides detailed metrics about memory, swap, IO, and CPU activity.

Terminal window
# Update every 2 seconds, 5 times total
vmstat 2 5

If you see high numbers in the si (Swap In) and so (Swap Out) columns, your server is heavily paging to the hard drive and desperately needs more physical RAM.

While they show CPU usage, they are equally important for sorting processes by Memory consumption.

  • In top, press Shift + M to sort processes by Memory usage.
  • Look at the RES (Resident) column to see exactly how much physical RAM a process is actually consuming.

Sometimes you need to add or disable Swap dynamically without reinstalling the OS.

Terminal window
# View all currently active swap devices
swapon --show

In modern orchestrators like Kubernetes (specifically kubelet), Swap is often strictly forbidden because it ruins predictable performance metrics.

Terminal window
# Disable all swap immediately (will move all swapped data back to RAM if there is room)
sudo swapoff -a
# Turn all swap devices defined in /etc/fstab back on
sudo swapon -a

If a server needs more memory immediately to survive a traffic spike:

Terminal window
# 1. Create a 2GB file filled with zeros
sudo fallocate -l 2G /swapfile
# 2. Secure it (only root should read it)
sudo chmod 600 /swapfile
# 3. Format it as Swap space
sudo mkswap /swapfile
# 4. Turn it on!
sudo swapon /swapfile