How to reliably measure available memory in Linux?

Alex B picture Alex B · Jun 11, 2010 · Viewed 12.9k times · Source

Linux /proc/meminfo shows a number of memory usage statistics.

MemTotal:      4040732 kB
MemFree:         23160 kB
Buffers:        163340 kB
Cached:        3707080 kB
SwapCached:          0 kB
Active:        1129324 kB
Inactive:      2762912 kB

There is quite a bit of overlap between them. For example, as far as I understand, there can be active page cache (belongs to "cached" and "active") and inactive page cache ("inactive" + "cached").

What I want to do is to measure "free" memory, but in a way that it includes used pages that are likely to be dropped without a significant impact on overall system's performance.

At first, I was inclined to use "free" + "inactive", but Linux's "free" utility uses "free" + "cached" in its "buffer-adjusted" display, so I am curious what a better approach is.

When the kernel runs out of memory, what is the priority of pages to drop and what is the more appropriate metric to measure available memory?

Answer

Tobu picture Tobu · Jun 13, 2010

Since what “available memory” precisely means depends on your purpose, and your purpose is to avoid OOM situations:

Check out how Qt Extended (previously Qtopia) anticipates OOM situations.

There are two events:

  • (MemFree + Buffers + Cached) / MemTotal < treshold (in /proc/meminfo)
  • Major pagefaults > treshold (pgmajfault in /proc/vmstat I think)

The first is an early warning that memory is low, and triggers more frequent monitoring of pagefaults. The second signals trashing, which kills system performance and is a good hint the OOM killer will run.