Unlock Linux Performance: Master Memory, Swap, and Cache Management
This guide explains Linux's memory architecture, how virtual memory (swap) works, and provides step‑by‑step commands to adjust swappiness, release page caches, and safely free swap space, helping you optimize system performance and avoid memory‑related issues.
What is Linux's memory mechanism?
Linux uses physical RAM for fast data access, but when RAM is insufficient it extends memory with virtual memory (swap) stored on disk. The kernel automatically moves rarely used pages to swap, keeping frequently accessed data in RAM, and employs a paging system to maximize RAM utilization.
Buffers and cache further improve performance: buffers store filesystem metadata, while cached holds recently accessed file data. When a file is requested, the system first checks these areas before reading from disk, dramatically speeding up I/O.
When does Linux start using swap?
The swappiness parameter controls swap usage. A value of 60 means swap is used when free RAM falls below 40% (100‑60). Setting swappiness=0 minimizes swap usage, while 100 forces aggressive swapping.
cat /proc/sys/vm/swappiness
60Typical recommendations: allocate swap equal to twice the RAM size for systems ≤4 GB, and at least equal to RAM for larger systems. Lowering swappiness generally improves performance.
Temporary change:
sysctl vm.swappiness=10
vm.swappiness = 10
cat /proc/sys/vm/swappiness
10Permanent change: edit /etc/sysctl.conf and add vm.swappiness = 35, then apply with sysctl -p /etc/sysctl.conf and verify using cat /proc/sys/vm/swappiness.
How to release memory?
Linux does not automatically free cached memory. The /proc/sys/vm/drop_caches file controls cache release; writing values 1‑3 triggers different levels of cleanup:
0 – no action
1 – free page cache
2 – free dentries and inodes
3 – free all caches
Example command (run as root): echo 3 > /proc/sys/vm/drop_caches The result is a noticeable increase in free memory, as illustrated in the accompanying screenshot.
How to release swap?
Before freeing swap, ensure that available RAM is at least equal to the current swap usage to avoid system crashes. Swap can be released by unmounting and remounting the swap partition. Typical steps include checking the swap device, turning it off, and then turning it back on.
Example sequence (replace /dev/sda5 with your swap device):
swapoff /dev/sda5
swapon /dev/sda5Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
