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.
