How to Calculate Memeory Usage
Normally we don’t calculate memory usage by ourselves. We can get percentage from system performance in Windows or System Monitor on GNOME. However, I am curious about how the percentage is calculated from back end.
We are all familiar with “top” or “htop” command in any Linux distribution. The memory usage is actually calculated from the raw data gotten from top command. The following screen shot is top running on my desktop.
As you can see that, I have 2G physical memory. I also have almost 4G swap memory which system allocated on hard disk for me. The physical memory is used 1769636K, which is actually divided to two parts. One parts is for all processes running on the system, the other part is for caching. We all know that Linux actively collect free physical memory to cache some instruction used by CPU. The idea behind caching is that it takes longer for your CPU to access data on the hard drive than it does to access data that is present in the main memory. So caching using the main memory effectively speeds up the system.
Therefore, we come up with two formula:
Cached Memory Usage = Cached Memory / Physical Total Memory
Program Memory Usage = (Used Physical Memory – Cached Memory ) / Physical Total Memory
The System Monitor on GNOME provide these two memory usage, which make us more clear about our desktop performance.

Some supplemental material for reference:
* sar is another common tool to give a intensive and historical view (i.e. since 12:00am the day) of resources using, include memory usage.
Try: “sar -r”
- %memused may usually close 100% means the system manages to utilize all real memory for better performance.
- kbcached shows amount of memory used to cache data by the kernel
- kbswpfree is bigger means you have more chance to do swap in the memory rather than I/O with hard drive, which means better performance.
- %swpused is lower means less requirement to do swapping (in memory), which means the system is more relax in paging.
“sar -r 2 10″ shows the current.
In addition, the “sar -R” will give an historical analysis for the paging (swap-in and out). man sar for descriptions of the columns. Generally there is performance punish when large number of in and out activities happening in considerable time range.
Same, “sar -R 2 10″ shows the current.
Some other useful commands:
* To show the top 10 processes by memory usage
# ps aux |head -1;ps aux | sort -nk +4 -r|head -10
* Who’s using the most of memory?
# ps -eo user,pcpu,pmem | tail -n +2 | awk ‘{num[$1]++; cpu[$1] += $2; mem[$1] += $3} END{printf(“NPROC\tUSER\tUSER\tCPU\tMEM\n”); for (user in cpu) printf(“%d\t%s\t%.2f\t%.2f\n”,num[user], user, cpu[user], mem[user]) }’
* To show the memory activity in conterpart with I/O and CPU usage:
# vmstat -Sm 2 10
e.g. low CPU usage while high “si” and “so” may contribute to high disk I/O (bi/bo) which leads to performance is suffering with “wa”(wait I/O) in result of high “r” and “b”.
If the cache is at the same time low as well as the active memory (shown by “vmstat -s”), it may point out lack of memory.
If the free memory is high at the same time, probably the application is not using out memory for caching and swapping – there would be a chance to visit paging (swapping) space settings or consider to force swap in memory by using virtual RAM disk as swapping (paging) device.
Hey Nan,
Thanks. Shamed that I didn’t update my blog very often right now, cuz currently I am busying on my job. I do plan to update some useful point to my blog, but it needs time to sort things out.
Take care buddy!
Henry