Master Linux Memory: Understanding Swap, Buffers, and How to Free RAM
This article explains Linux's memory architecture, the role of virtual memory and swap, the distinction between buffers and cache, and provides practical commands to adjust swappiness, clear caches, and safely release both RAM and swap space for improved system performance.
Linux memory management involves physical RAM, virtual memory (swap), and caching mechanisms (buffers and cache) to optimize performance and handle limited physical memory.
1. What is Linux's memory mechanism?
Physical memory is the actual RAM provided by hardware, while virtual memory extends it using disk space called swap. When RAM is insufficient, the kernel moves rarely used pages to swap, freeing RAM for active processes. Linux uses a paging system and a "recently used" algorithm to decide which pages to swap.
Buffers store metadata of block devices (e.g., directory entries, permissions) and dentries/inodes, whereas cache holds copies of file contents that have been read, speeding up subsequent accesses.
2. When does Linux start using virtual memory (swap)?
The swappiness parameter controls swap usage. A value of 60 means swap is used when free RAM falls below 40% of total memory. Setting it to 0 minimizes swap usage, while 100 makes the system aggressively use swap.
cat /proc/sys/vm/swappiness
60Typical recommendation: set swap size to twice the RAM if RAM ≤ 4 GB; otherwise, swap should be larger than RAM. Lowering swappiness generally improves performance.
Temporarily change swappiness:
sysctl vm.swappiness=10
vm.swappiness = 10
cat /proc/sys/vm/swappiness
10Permanently change swappiness:
vim /etc/sysctl.conf # add line: vm.swappiness = 35
sysctl -p /etc/sysctl.conf
cat /proc/sys/vm/swappiness # should show 353. How to free memory?
Linux does not automatically free cached memory. The /proc/sys/vm/drop_caches file controls cache release:
0 – Do not free
1 – Free page cache
2 – Free dentries and inodes
3 – Free all caches
Example command (run as root):
echo 3 > /proc/sys/vm/drop_cachesAfter executing, you should see a noticeable increase in free memory.
4. How to free swap?
Ensure that free RAM is at least equal to the amount of swap you plan to release; otherwise the system may crash. To release swap, deactivate the swap partition and reactivate it:
swapoff -a
swapon -aOr manually unmount and remount a specific swap device (e.g., /dev/sda5):
swapoff /dev/sda5
swapon /dev/sda5Properly managing memory and swap helps maintain system stability and performance.
Signed-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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
