Linux and free memory

Posted by on Feb 21, 2011 in Linux, Unix | No Comments

How much memory really is free on a Linux system, anyway?

This is something that I have seen cause confusion in the past. A DBA, developer, or consultant is trying to figure out why a particular application or program is running poorly (it’s never, ever their Java or SQL code), runs top or free, sees very little free memory, and jumps to the conclusion that the system is out of memory.  And the buck is inevitably passed.  To you.

So, how the heck do you find out how much memory is *really* being used?  One way is to view /proc/meminfo:

MemTotal:     32904572 kB
MemFree:         92028 kB
Buffers:        196172 kB
Cached:       24069564 kB
SwapCached:        336 kB
Active:       20241392 kB
Inactive:      9761156 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:     32904572 kB
LowFree:         92028 kB
SwapTotal:    22020088 kB
SwapFree:     21883304 kB
Dirty:             392 kB
Writeback:           0 kB
Mapped:        9632036 kB
Slab:           569292 kB
CommitLimit:  38472372 kB
Committed_AS: 14082748 kB
PageTables:    2170840 kB
VmallocTotal: 536870911 kB
VmallocUsed:    276288 kB
VmallocChunk: 536594487 kB
HugePages_Total:     0
HugePages_Free:      0
Hugepagesize:     2048 kB

As we can see, the system has 32 GB of memory, but less than 90MB of “free” memory.  Oh noes!  But as with most things Unix, if you understand what’s going on, this all makes quite a bit more sense.  Let’s zero in on a few, key numbers here:

MemTotal:     32904572 kB

This is the total amount of memory in the system.  Pretty straightforward.

Active:       20241392 kB

This is the amount of memory actively being used by the system, in this case just less than 20 GB.  Notice that subtracting this number from the total does not equal the following:

MemFree:         92028 kB

So if memory’s not actively being used, and it’s not free, then what the heck is going on?  Part of it is that the Linux kernel is being smart and keeping things in memory that it was using, but isn’t now.  That way, in case you need the data again, it is sitting there in nice, fast memory instead of on slow disk.

Inactive:      9761156 kB

“Inactive” memory is memory that hasn’t been recently used and is eligible for cleaning.  But for now, it’s being used as a cache and also to confuse your dev team.

Dirty:             392 kB

“Dirty” memory is memory that doesn’t match what’s on disk and probably needs to be flushed.

For further reading, see Red Hat Magazine, “Understanding Virtual Memory.”  It’s for RHEL 3, but probably still fairly accurate.