為什麼 free 很小,Linux 卻還能開程式?看懂 MemFree 與 MemAvailable / Why a Tiny free Column Does Not Mean Linux Cannot Start Programs
現象:free 很小,不代表開不了程式 / A tiny free column is not “cannot start”
假設你在 Linux 終端機打出 free,free 欄只剩幾十 MB,used 卻佔了一大半。很容易先想到「記憶體爆了,得重開或加 RAM」。先停一下:這個畫面常常不是實體記憶體已經沒了,而是把「完全沒被用到的頁」,當成「還能開新程式的空間」。
Suppose you run free in a Linux terminal and the free column is down to a few dozen megabytes, while used takes most of the row. It is easy to jump to “memory is exhausted; reboot or add RAM.” Pause first: that picture often does not mean physical memory is gone. It means idle pages have been mistaken for the space still available to start a new program.
/proc/meminfo 的 MemFree,依 Linux 7.4-rc1 的 proc 文件,是目前的 free RAM;在 highmem 系統上,它是 LowFree 與 HighFree 的和。它變小,只表示幾乎沒有完全閒置的頁。核心會把讀過的檔案留在記憶體裡,這份快取叫 page cache,也就是頁快取。那些頁還佔著 RAM,所以 MemFree 下降,但它們不是某個程式私有、放不掉的佔用。[1]
In Linux 7.4-rc1’s proc documentation, MemFree in /proc/meminfo is the current free RAM; on highmem systems it is the sum of LowFree and HighFree. A small value only means there are almost no completely idle pages. The kernel keeps files it has read in memory; that cache is the page cache. Those pages still occupy RAM, so MemFree falls, but they are not private, unreleasable use by a particular program. [1]
MemAvailable 是估計,不是空白 / MemAvailable is an estimate, not empty space
要看「還能不能開新程式」,文件給的欄位是 MemAvailable。man-pages 6.19 註明它自 Linux 3.14 起出現,定義是:在不換頁的前提下,估計還能用來啟動新應用程式的記憶體。換頁(swapping)是把記憶體頁暫存到交換空間。這份估計由 MemFree、SReclaimable、file LRU 清單的大小,以及各 zone 的 low watermark 算出,並承認系統仍需留一些 page cache 才能好好運作,也不是所有可回收的 slab 都真的收得回來。所以它是估計,不是量到的空白,機器之間的差距也會不一樣。[1][2]
The field for “can a new program still start” is MemAvailable. man-pages 6.19 notes that it has existed since Linux 3.14, and defines it as an estimate of memory available for starting new applications without swapping. Swapping means parking memory pages in swap space. The estimate is calculated from MemFree, SReclaimable, the size of the file LRU lists, and the low watermarks in each zone. It allows that the system still needs some page cache to function well, and that not all reclaimable slab can actually be reclaimed because items are in use. It is an estimate, not measured empty space, and the gap varies from machine to machine. [1][2]
還有一個容易加錯的欄位。同一份核心文件寫,Cached 是從磁碟讀入的 page cache,也包含 tmpfs 與 shmem,但不含 SwapCached。man7 的 proc_meminfo 頁只寫 page cache、不含 SwapCached,沒有寫出 tmpfs 與 shmem。兩邊都排除 SwapCached;是否包含 tmpfs,以核心文件為準。把 MemFree 直接加上整個 Cached,會把不一定能立刻讓出來的共用記憶體也算進去。[1][2]
Another field is easy to add in the wrong way. The same kernel document says Cached is the in-memory cache for files read from disk, the page cache, and also includes tmpfs and shmem, but not SwapCached. The man7 proc_meminfo page says page cache and excludes SwapCached, without mentioning tmpfs or shmem. Both exclude SwapCached; for whether tmpfs is included, the kernel document is the one to follow. Adding all of Cached to MemFree counts shared memory that cannot necessarily be given back at once. [1][2]
used 欄要對版本 / The used column depends on the version
free 這個指令是在整理 /proc/meminfo,不是另一套記憶體帳。兩邊手冊都寫:available 對應 MemAvailable,會把 page cache 算進去,也承認並非所有可回收 slab 都會被收回。used 的算法卻要看版本。Debian bookworm 的 procps 2:4.0.2-3 寫成 total 減 available;這台 WSL2 的 procps-ng 3.3.17 手冊則寫成 total 減 free 減 buffers 減 cache。同一欄名字,兩套說明不是同一個數。看「還能不能開程式」,對的是 available,不是把 used 當成唯一答案。[3][4]
The free command formats /proc/meminfo; it is not a second set of memory accounts. Both manuals say available corresponds to MemAvailable, takes page cache into account, and accepts that not every reclaimable slab will be reclaimed. The formula for used depends on the version. Debian bookworm’s procps 2:4.0.2-3 describes it as total minus available. The procps-ng 3.3.17 manual on this WSL2 describes it as total minus free minus buffers minus cache. The same column name is not the same number in the two manuals. For “can a new program start,” read available, not used as the only answer. [3][4]
唯讀對一下 / A read-only check
在 Linux 上,唯讀對一下就夠:用 grep 讀 /proc/meminfo 的 MemFree、MemAvailable 與 Cached,再跑 free -k,最後用 man free 看這台機器的 used 是哪一種算法。這些指令不改系統。本文的數字只來自 2026-11-03 在這台 Windows 的 WSL2(Linux 6.6.87.2-microsoft-standard-WSL2)做的一次觀察,不是壓力測試,也不代表別台機器。當時 free -k 的 Mem 列是 total 16253080、used 1131716、free 11766204、buff/cache 3355160、available 14804828,單位 KiB;同一串指令稍後讀到 MemFree 11765956 kB、MemAvailable 14804588 kB。兩次讀取隔了幾秒,所以不會完全相同。這組快照的 free 本來就不低,沒有示範「free 快見底」,只核對 3.3.17 的算法:used、free、buff/cache 三者相加等於 total。
On Linux, a read-only check is enough: grep MemFree, MemAvailable, and Cached from /proc/meminfo, run free -k, then read man free to see which used formula that machine documents. These commands do not change the system. The numbers here come from one observation on 2026-11-03 in WSL2 on this Windows machine (Linux 6.6.87.2-microsoft-standard-WSL2). It was not a stress test, and it does not describe other machines. At that moment the Mem row of free -k was total 16253080, used 1131716, free 11766204, buff/cache 3355160, available 14804828, all in KiB. A moment later in the same command, MemFree was 11765956 kB and MemAvailable was 14804588 kB. The two reads were seconds apart, so they do not match exactly. Free was not low in this snapshot, so it does not demonstrate “free is nearly gone.” It only checks the 3.3.17 formula: used, free, and buff/cache add up to total.
# Linux;只讀,不寫入 / read-only, no writes
grep -E '^(MemTotal|MemFree|MemAvailable|Cached|SReclaimable):' /proc/meminfo
free -k
man free # 先看這台機器的 used 算法 / read this machine's used formula回到那個「記憶體快用完」的畫面,可以分開三件事。MemFree 低,表示閒置頁少;MemAvailable 仍夠,表示估計還能在不換頁的情況下開新程式;若 available 也一起掉,而且系統開始大量換頁,那是另一個問題,本文不把它說成同一個原因。下次看到 free 很小,先讀 MemAvailable,再決定要不要處理。
Back to the “memory is almost gone” screen, three things can be kept apart. Low MemFree means few idle pages. MemAvailable still being large means the estimate says a new program can start without swapping. If available falls too, and the system starts swapping heavily, that is a different problem; this article does not treat it as the same cause. Next time free looks tiny, read MemAvailable before deciding whether anything needs to be done.
留言
張貼留言