Neeraj Bhatia's Blog

October 25, 2010

Linux Memory Utilization

Filed under: Linux OS — neerajbhatia @ 09:08

Many times I observed that novice database/system administrators on Linux platform confuse with the actual memory utilization and utilization presented by Linux native utilities like sar, top etc. The reason behind this is, the way Linux presents memory utilization.

While performance diagnosis most of the times you’ll find 90-100% memory utilization, however that may not be the case and you can calculate “actual” memory utilization in following fashion.

Fetch memory utilization data from sar files using -r switch.
sar -r -f <filename>

It will display data in this format


04:40:01 PM kbmemfree kbmemused %memused kbbuffers kbcached kbswpfree kbswpused %swpused kbswpcad
04:50:01 PM 258788 16372260 98.44 12696 13912224 12484624 94144 0.75 420

Information being displayed here, is somewhat misleading. According to it, at 04:50PM, 98.44% memory was utilized (As its merely calculated by the formula kbmemused/sum(kbmemused, kbmemfree)).

But in actual that was not the case. In order to get actual memory utilization, subtract kbbuffers and kbcached from kbmemused, and then used the above formula. In this case, Memory utilization = (16372260-12696-13912224)/(258788+16372260) = 14.71%

The reason behind this is Linux treats unused memory as a wasted resource and so uses as much RAM as it can to cache process/kernel information.

Here Buffers= amount of physical memory used as buffers for disk writes Cached = amount of physical memory used as cache for disk reads

Even if you use top utility to check the memory utilization, situation seems dreadful. It appears that almost all memory is being used. This is because of the way top display memory utilization information.

Here is an example:


top - 09:43:51 up 29 min,  2 users,  load average: 0.10, 0.27, 0.22

Tasks: 166 total,   2 running, 164 sleeping,   0 stopped,   0 zombie

Cpu(s):  5.9%us,  0.8%sy,  0.0%ni, 93.1%id,  0.2%wa,  0.0%hi,  0.0%si,  0.0%st

Mem:   2065628k total,  1195636k used,   869992k free,    59540k buffers

Swap:  2104472k total,        0k used,  2104472k free,   695492k cached

The top command reports that total 2065628k bytes (2017MB) of memory is configured on the system, 1195636k (1167MB) is used and 869992k (849MB) is free. Some of the used memory that the top command reports is actually being used for cache. In other words, it is available to be reassigned if another process requires it. To get a better picture of memory utilization, you should use free command. free is basically a c-program that reads output from /proc virtual file system.

Here is an example:


total       used       free     shared    buffers     cached

Mem:          2017       1180        836          0         59        690

-/+ buffers/cache:        430       1586

Swap:         2055          0       2055

The free command reports output in 1024-byte blocks. I used -m switch to report memory utilization in megabytes.

We can interpret free command output as follows. In the first line, it reports that this system has 2017MB memory configured, out of which 1180MB is used and 836 is free. The shared memory column is obsolete and should be ignored. 59MB reported under buffers column, being used as buffers for disk writes and 690MB used as cache for disk read operations. It should be note that memory reported under buffers and cached, is available for use. Hence in second line, they are added to free memory. Thus in actual, system has 1586MB free for use (~ =836+59+690 MB).
Thus it is important to note that, we should concentrate on second line “-/+ buffers/cache:” to get free memory in the system.
The third line reports swap utilization information. Here system has 2055MB of swap configured, swap is not being used and total swap is free for use. We can also get current memory utilization using vmstat (virtual memory statistics tool).

I hope it will help you to get better picture of memory utilization.

Cheers!

The Rubric Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 159 other followers