Why Using Swap Is Dangerous
Swap is a technology that uses a portion of a hard disk or SSD as memory when physical RAM is insufficient.
The Linux kernel pushes memory pages that are not immediately in use to disk (swap-out) and retrieves them when needed.
While swap does allow for more space to be used, accessing swapped-out memory causes performance degradation. This is because it's secondary storage.
- Non-deterministic Performance: API responses might be fast at certain times but suddenly take several seconds.
- Disk Thrashing: The CPU remains idle while disk I/O hits 100%, effectively freezing the system.
- False Positive State: Processes remain alive but unresponsive, becoming zombie states that pass load balancer health checks.
Latency Gap #
RAM access speeds are in nanoseconds (ns), whereas SSDs and HDDs are in milliseconds (ms).
Even with a simple calculation, this is a 100,000-fold difference. The moment swapping occurs, application performance doesn't just slow down; it effectively halts.
OOM Killer Delay #
Linux has an OOM Killer that forcibly terminates processes when memory is low to protect the system.
If swap is enabled, the kernel tries to survive by using swap instead of killing processes. As a result, the system doesn't fail immediately but dies very painfully, affecting the entire service.
Compatibility with GC #
Java JVM and Go runtimes perform garbage collection by scanning the entire memory.
If part of the memory is in swap, disk I/O occurs every time GC runs. A GC that would normally finish in 10ms could take over 10 seconds, leading to prolonged Stop-The-World (STW) pauses.
Therefore, by preventing swap, you can achieve predictable performance. Instead of enduring with swap when memory is low, allowing OOM to occur clarifies the cause of the failure. This enables immediate response.
It also prevents the "noisy neighbor" problem, where a memory leak in a specific process monopolizes the entire system's I/O, paralyzing other healthy services.
Checking and Controlling Swap Status #
First, check the overall memory status of the system.
free -h
total used free shared buff/cache available
Mem: 15Gi 12Gi 500Mi 1.0Gi 2.5Gi 2.0Gi
Swap: 2.0Gi 1.5Gi 500Mi # <--- Caution! 1.5Gi of Swap is in use.
Track which processes are using it.
# Output system report every 1 second
vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 1 1572864 512000 10240 524288 150 200 10 50 500 800 10 5 80 5 0
- si (swap in): Amount read from disk to memory.
- so (swap out): Amount pushed from memory to disk.
- Interpretation: If
siandsovalues are greater than 0 and occur continuously, it indicates that disk thrashing is currently happening.
Checking and Changing Swappiness Setting #
This parameter determines how aggressively the kernel uses swap, ranging from 0 to 100. Lower values suppress swap usage.
cat /proc/sys/vm/swappiness
# Result: 60 (Linux default, too high for server environments)
# Temporarily change to 10 (encourages maximum RAM usage)
sudo sysctl vm.swappiness=10
You can also disable it entirely. In Kubernetes environments or high-performance database servers, completely turning off swap is an option.
# Disable all Swap devices
sudo swapoff -a
# Comment out swap-related lines in /etc/fstab to apply after reboot
sudo vi /etc/fstab
# /swapfile none swap sw 0 0 <-- Add # before this line to comment it out
Swap is not insurance. While swap is often seen as a safety net for when memory is low, in practice, it's a choice between dying slowly and painfully, or dying quickly and recovering.
Dying slowly and painfully is swap; dying quickly is OOM. From an availability perspective, the latter is more advantageous.
It's better to set an alarm when Memory Usage > 85% and respond proactively, rather than setting a threshold for when swap-out occurs.
For Kubernetes, it is officially recommended to disable swap on nodes.
This is because the scheduler does not consider swap when calculating available resources on a node.