Monday, July 16, 2012

Process memory usage percentage not computing properly

In a scenario when i want to compute the percentage of a process memory consumption against the total memory installed, I encountered a logic error where in the result is zero. The simple formula is this:
Code:
double x = p.PrivateMemorySize64 / installed_memory_pc * 100; 

However, the result in x is zero. The trick is to convert p.PrivateMemorySize64 and installed_memory_pc to megabytes by dividing each variable with 1048576. 1Mb = 1048576 bytes. The modified formula is:
Code:
mbInstalledMemory = (int)installedMemory / 1048576;  
mbUsage = (int)p.PrivateMemorySize64 / 1048576;
Then you can apply the percentage formula.
Cheers!

2 comments:

  1. Interesting post! I enjoyed reading it!
    Thanks for sharing this useful info.keep updating same way.
    Cheers,
    Ramesh Kr
    Dot Net Software Development

    ReplyDelete