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.
20.1 Core Memory Concepts
Section titled “20.1 Core Memory Concepts”Virtual Memory
Section titled “Virtual Memory”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 vs Swap
Section titled “RAM vs Swap”- 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.
Buffers and Page Cache
Section titled “Buffers and Page Cache”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.
20.2 The OOM Killer
Section titled “20.2 The OOM Killer”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).
20.3 Memory Monitoring Commands
Section titled “20.3 Memory Monitoring Commands”The quickest way to check current memory usage.
# View memory in Human-readable format (MB/GB)free -hPay 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.
vmstat (Virtual Memory Statistics)
Section titled “vmstat (Virtual Memory Statistics)”Provides detailed metrics about memory, swap, IO, and CPU activity.
# Update every 2 seconds, 5 times totalvmstat 2 5If 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.
top & htop
Section titled “top & htop”While they show CPU usage, they are equally important for sorting processes by Memory consumption.
- In
top, pressShift + Mto sort processes by Memory usage. - Look at the
RES(Resident) column to see exactly how much physical RAM a process is actually consuming.
20.4 Managing Swap
Section titled “20.4 Managing Swap”Sometimes you need to add or disable Swap dynamically without reinstalling the OS.
Checking Swap
Section titled “Checking Swap”# View all currently active swap devicesswapon --showTurning Swap On/Off
Section titled “Turning Swap On/Off”In modern orchestrators like Kubernetes (specifically kubelet), Swap is often strictly forbidden because it ruins predictable performance metrics.
# 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 onsudo swapon -aCreating a Swap File Dynamically
Section titled “Creating a Swap File Dynamically”If a server needs more memory immediately to survive a traffic spike:
# 1. Create a 2GB file filled with zerossudo fallocate -l 2G /swapfile
# 2. Secure it (only root should read it)sudo chmod 600 /swapfile
# 3. Format it as Swap spacesudo mkswap /swapfile
# 4. Turn it on!sudo swapon /swapfile