Memory Usage under Linux
GEEKS !! Welcome back after a really long “Diwali” holiday.
Lets start with something very interesting. Lets understand in depth the memory usage of process in Linux.
I know .. I know you know “ps” command. Which you have used in your RHCE and RHCSS Training. That gives you information about the process running and the memory it is consuming. Apart from giving information like process id, current running state, and resource utilization. The two most important outputs in “ps” command are –
VSZ – Virtual Set Size
RSS – Resident Set Size
These two fields are used by us to check how much memory a process is taking.
Now READ CAREFULLY.
PS is wrong. WHY??
Actually, GEEKS, ps is never reporting the real memory usage of processes.
“It is showing how much real memory that process would take up if it was the only process running.”
Now we know that can never be the case. Our Linux server might be and will be running umpteen number of processes or applications. So that means that VSZ and RSS numbers are surely “wrong”. WHY .. WHY ???
Lets understand how our loving Linux handles shared libraries.
Most major programs on Linux use shared libraries to facilitate certain functionality. For example, one text editing GNOME program will use several GNOME shared libraries (to allow for interaction with other GNOME components), several X libraries (to allow it to display images and copy and pasting), and several general system libraries (to allow it to perform basic operations).
Many of these shared libraries, especially commonly used ones like libc, are used by many of the programs running on a Linux system.
Due to this sharing, Linux is able to do a MAGIC trick: it will load a single copy of the shared libraries into memory and use that one copy for every program that references it.
Now here is the problem, many tools (including ps) don’t care very much about this very magical trick; they simply report how much memory a process uses, regardless of whether that memory is shared with other processes as well. Two programs could therefore use a large shared library and yet have its size count towards both of their memory usage totals; the library is being double-counted, which can be very misleading if you don’t know what is going on.
So what is the crux of the story?
GEEKS the crux is that process memory usage on Linux is a complex matter; you can’t just simply run ps and know what is going on. This is especially true when you deal with programs that create a lot of identical children processes, like Apache. ps might report that each Apache process uses 10 or 12 mb of memory, when the reality might be that the marginal cost of each Apache process is 1 megabyte of memory. This information can be very important when tuning Apache for say configuring Apache’s MaxClients setting, which determines how many simultaneous requests your server can handle.
So who can help?
Well.. you can use valgrind. But one more easy tool way is to use a combination of “pidof” and using cat command to view the run time status of memory usage of a process.
And you have the liberty to use “pmap -x processid” also.
Here is a illustrated example from my machine.