Linux Memory Explained: free, top and What to Do When RAM Runs Out


Every Linux beginner has the same panic moment: they run free -h, see 95% of RAM “used”, and assume something is broken. Usually it is not. Linux deliberately uses spare RAM as disk cache, and that cache is freed instantly when programs need it. The trick is telling healthy caching apart from genuine memory pressure — and knowing what to do about the second one.

Reading free the Right Way

free -h

The columns that matter:

  • used — memory held by programs. On its own, this number misleads you.
  • buff/cache — disk cache. This is Linux being smart, not wasteful. It speeds up file access and is reclaimed on demand.
  • available — the number to actually watch. This is how much memory programs can still get, including cache that can be freed. If available is healthy, you are fine, whatever used says.

Rule of thumb: worry about available approaching zero, not about used approaching total.

Finding the Hungry Process

When memory really is tight, find the culprit:

ps aux --sort=-%mem | head -n 10
top (then press Shift+M to sort by memory)

Look for a process whose memory grows steadily over hours — that pattern is a memory leak, and the fix is restarting (or updating) that service, not adding RAM forever.

Swap: Emergency Room, Not Extra RAM

Swap is disk space used as overflow memory. It keeps a system alive when RAM fills up, but disk is vastly slower than RAM, so heavy swapping (called thrashing) makes everything crawl. Check it with:

swapon --show
vmstat 1 5

If si (swap in) and so (swap out) stay high, your system is thrashing. That is a real problem, not a curiosity.

What to Do When RAM Actually Runs Out

  • Restart the leaking service — sudo systemctl restart servicename buys immediate relief and confirms the diagnosis if memory drops.
  • Tune, don’t just add RAM. A web server spawning too many workers or a database with an oversized cache will eat any amount of RAM you give it. Fix the config.
  • Set limits. systemd lets you cap a service’s memory so one runaway process cannot take the whole server down (use sudo systemctl edit servicename, add MemoryMax=1G in the override).
  • Check the OOM killer’s victims. When Linux runs out completely, the kernel kills a process to survive:
sudo dmesg | grep -i "killed process"
sudo journalctl -k | grep -i oom
  • Add swap as a safety net, not a solution. A modest swap file prevents sudden kills on small servers, but if you live in swap, you need more RAM or fewer services.

Security Angle

Memory pressure is also an attack surface. A service that accepts unbounded uploads, unclosed connections or unlimited worker processes can be driven into memory exhaustion — a denial-of-service with no exploit needed. Caps, timeouts and connection limits are security controls, not just performance tuning. Stop fearing the used column. Watch available, find the real hog, and fix the cause. That is Linux memory management in one paragraph.

Comments

Popular Posts