Centos – Why does ‘top’ indicate low memory usage, whilst ‘free’ indicates high memory usage

centosmemory usagetopvps

Why does 'top' indicate low memory usage, whilst 'free' indicates high memory usage?

Mem:    262144k total,   225708k used,    36436k free,    47948k buffers
Swap:   262136k total,       40k used,   262096k free,   110704k cached

 PID  USER      PR   NI VIRT  RES  SHR  S %CPU %MEM  TIME+   COMMAND                         
 1652 root      15   0  79456 14m  1728 S 0.0  5.6   0:00.02 miniserv.pl                  
 3544 root      15   0  87920 3356 2584 R 0.0  1.3   0:00.01 sshd  
 3707 root      16   0  86704 3104 2416 S 0.0  1.2   0:00.00 sshd    
 3708 sshd      15   0  61864 1452 872  S 0.0  0.6   0:00.00 sshd          
 3548 root      16   0  10872 1432 1116 S 0.0  0.5   0:00.00 bash                      
 1908 root      15   0  10876 1392 1072 S 0.0  0.5   0:00.00 sh      
  918 root      15   0  60520 1204 664  S 0.0  0.5   0:00.07 sshd                        
 3624 root      15   0  12584 1180 920  R 0.0  0.5   0:00.00 top                       
  926 root      18   0  19672 1152 576  S 0.0  0.4   0:00.00 crond         
    1 root      15   0  10324 704  588  S 0.0  0.3   0:00.02 init               
  311 root      16  -4  12580 704  388  S 0.0  0.3   0:00.06 udevd                       
  874 root      16   0  5884  592  472  S 0.0  0.2   0:00.06 syslogd                   
  877 root      18   0  3780  420  336  S 0.0  0.2   0:00.00 klogd

Total MEM usage = 12.1%

However, 226mb used out of 262mb, with 36mb free – i.e. 86.25% used.

Even taking in to account the swap, total memory 262mb*2 = 524mb. Given 226mb is used, 43.12% is used. Both are much higher than the actual processed listed by top.

The server is a fresh 256MB VPS running CentOS 5.2 – haven't installed anything on it yet. Running webadmin; i.e. no Plesk.

This question is fundamentally about the conflicting information that seems to be given by top; but I am also wondering if this memory usage is normal, for a server that isn't running any particularly memory intensive applications. I intend to install Apache/Ruby EE/Passenger; however if memory usage is already this high, I'm unsure how well it is going to handle it.

Best Answer

Top, that is the figure in the %MEM column, is counting the amount of RSS memory (Resident Segment Size, basically pages physically in memory that have real data on them) as a percentage of total physical memory in your machine or VPS.

On the other hand, free is counting just that, the amount of physical memory pages that have no data on them, and have not been assigned to buffers, cache or the kernel. In a Unix like operating system, the OS tries hard to keep that number as low as possible by using free pages for disk cache. The only time you'll likely a high value of free memory is just after your machine boots, or if you quit a program that was consuming a large amount of physical memory itself.

Is this memory usage normal ? The short answer is yes. It is typical for Unix programs to allocate (that is ask the OS for) significantly more memory than they would use. If you look at the VSS column, for the processes listed the total is over 463mb. That is because

  • A lot of the memory accounted against each process will be physically mapped to the same library, say glibc
  • The OS generally overcommits memory to the application, on the basis that most applications never come to collect on what they have asked for.

Figure out process memory usage is more an art than a science IMHO, see the discussions on http://lwn.net. My advice is to keep a close eye on iostat -xm and ensure that your machine is not swapping heavily.