Check Memory with free
free -h
The -h flag prints sizes in readable binary units such as Mi and Gi. The output has a line for memory and a line for swap, with columns like these (your numbers will differ):
total used free shared buff/cache available
Mem: 3.8Gi 1.2Gi 410Mi 45Mi 2.3Gi 2.4Gi
Swap: 2.0Gi 0B 2.0Gi
Add -s 2 to repeat the output every two seconds, or -m to print everything in MiB.
What Each Column Means
| Column | Meaning |
|---|---|
total | Memory the system can use. |
used | Total minus available: memory in use. |
free | Memory not used for anything at all. |
shared | Mostly tmpfs and shared memory segments. |
buff/cache | Disk cache and kernel buffers. |
available | Estimate of memory new work can get without swapping. |
Free vs Available: The Common Misreading
A server that has run for a few days often shows only a few hundred megabytes in the free column. That is not a shortage. Linux keeps recently read files in memory so the next read does not touch the disk, and it drops that cache as soon as a program asks for memory. The available column is the kernel's estimate of free memory plus the cache and other memory it can reclaim, so it is the number to watch.
Memory is really short when available stays low for a long time, swap use keeps growing, and the server becomes slow. A low free value on its own means the cache is doing its job.
Read /proc/meminfo
free reads its numbers from this file. To see the main lines:
grep -E 'MemTotal|MemFree|MemAvailable|^Cached|SwapTotal|SwapFree' /proc/meminfo
- MemTotal: usable memory.
- MemFree: memory that holds nothing; the same as the free column.
- MemAvailable: the kernel's own estimate behind the available column.
- Cached: the page cache for files.
- SwapTotal and SwapFree: swap size and how much of it is unused.
Watch Memory over Time with vmstat
vmstat 1 5
This prints one line per second, five times. The memory columns are swpd (swap in use), free, buff, and cache. The two columns that matter most are si and so: memory swapped in from and out to disk each second. Occasional small values are harmless; steady non-zero values mean the server is short of memory and is paying for it with disk traffic.
Find the Processes That Use the Most Memory
In top, press M (Shift+m) to sort by memory. In htop, press M, or press F6 and pick the memory column. For a list you can copy into a ticket or a note:
ps -eo pid,user,rss,%mem,comm --sort=-rss | head -n 11
RSS (resident set size) is the memory a process holds in RAM, in KiB. Several worker processes of one program, such as PHP-FPM or a database, each show their own line; add them up to see the program's total. Shared pages are counted in each process, so the sum is an upper bound. The top command and htop guides explain both tools, and how to list processes in Linux covers ps in more detail.
When Memory Really Runs Out
- Swap fills and si/so stay high: the server needs less load or more memory.
- A process disappears: the kernel may have stopped it to save the system; see the OOM killer.
- No swap at all: a small swap file gives a cushion against short peaks; see how to add a swap file.
Swap is a cushion, not extra memory. If available is low every day at normal load, reduce what the services are allowed to use or move to a plan with more RAM.