SWAP is not just for full RAM: kernel memory mechanics and a 4GB setup#
During a routine production host audit, I checked storage utilization with df -h:
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 99G 72G 28G 73% /
/dev/sda2 994M 661M 334M 67% /boot
/dev/sda1 99M 5.8M 94M 6% /boot/efi
The root partition had 28 GB available and RAM utilization was stable. However, a critical issue stood out: there was no active SWAP configured.
A frequent question arises in this situation: "If we have 28 GB of free disk and physical RAM is not exhausted, do we really need SWAP?"
The answer is yes. Here is an explanation of the Linux kernel memory management mechanics behind this rule and how to set up a 4GB swapfile.
1. The myth of SWAP as merely "emergency slow RAM"#
Many view SWAP solely as a fallback mechanism for when physical RAM runs out. In the Linux kernel, SWAP serves a broader purpose: balancing Least Recently Used (LRU) memory pages.
Page cache (file-backed) vs. anonymous pages#
The kernel divides active memory into two main types:
- File-backed pages (Page Cache): Cached disk files, shared libraries, and application binaries.
- Anonymous pages: Memory allocated dynamically by running processes (heap, stack, database memory pools) without direct file backing on disk.
When a system runs without SWAP, the kernel is strictly forbidden from evicting idle anonymous pages to disk. To maintain free operating memory, it is forced to aggressively purge the Page Cache.
This introduces an immediate performance penalty: disk read latency spikes because binaries and files must be repeatedly re-read from storage.
The swappiness calculation and OOM killer risks#
In the kernel source code (mm/vmscan.c), the get_scan_count function uses vm.swappiness (typically 60) to balance page cache eviction against anonymous page paging. With zero SWAP configured, this balance is broken.
During sudden traffic spikes (bursts in web servers or database queries), systems without SWAP can trigger the Linux OOM Killer (Out Of Memory Killer) prematurely, killing processes like SSHD or MySQL without notice. This behavior is frequent in servers running runtimes like Node.js and Bun under PM2 or cloud VPS instances like Oracle Cloud OCI.
2. Low-level terminal diagnostics#
To evaluate memory pressure before creating SWAP:
A. Pressure stall information (PSI)#
cat /proc/pressure/memory
B. Virtual memory paging and i/o wait#
vmstat 1 10
Monitor the si (swap-in), so (swap-out), and wa (I/O wait) columns to confirm whether the lack of SWAP is causing storage queue saturation.
C. Memory page allocation breakdown#
grep -E 'AnonPages|Buffers|Cached' /proc/meminfo
3. Sizing and binary alignment (4 gib)#
For this environment, I sized a 4 GB swapfile:
- Decimal base (storage metric): $4\text{ GB} = 4,000\text{ MB}$
- Binary base (kernel / MMU): $4\text{ GiB} = 4 \times 1024\text{ MiB} = 4,096\text{ MiB}$ (or $4,194,304\text{ KB}$)
Because the x86_64 MMU manages memory in 4 KB pages, allocating in binary multiples ensures clean page block alignment.
4. Creating and enabling the swapfile#
Step 1: Preallocate space with fallocate#
The fallocate command allocates blocks instantly across EXT4/XFS filesystems:
sudo fallocate -l 4G /swapfile
Step 2: Set strict security permissions#
Because the swapfile will store in-flight memory pages, access must be restricted to root:
sudo chmod 600 /swapfile
Step 3: Format and enable#
sudo mkswap /swapfile
sudo swapon /swapfile
Verify the active configuration:
swapon --show
free -h
Step 4: Persist in /etc/fstab#
Add the mount line to /etc/fstab:
/swapfile swap swap defaults 0 0
5. Edge cases and best practices#
- Block fragmentation: To verify the file was allocated contiguously on disk, run
filefrag -v /swapfile. - Fast NVMe vs. Thrashing: On modern SSD or NVMe drives, occasional paging operations complete quickly. SWAP acts as a safety buffer allowing physical RAM to retain high-value file caches.
Practical lessons on Linux memory management#
- SWAP is not just for memory exhaustion: Having swap space allows the kernel to preserve the Page Cache and optimize I/O efficiency across the system.
- Use fallocate and chmod 600: Fast preallocation saves setup time, and strict permissions prevent unauthorized access to memory pages on disk.
- Always persist in /etc/fstab: Register the swapfile in your filesystem table to ensure memory protections remain active across reboots.
Was this article helpful?
Leave a quick reaction to help prioritize future technical guides:
This post is licensed under CC BY-NC.



Comments
Join the discussion below.
0 comments