Process Memory Usage Percentage Not Computing Properly In C#
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:
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:
Then you can apply the percentage formula.
Cheers!
double x = p.PrivateMemorySize64 / installed_memory_pc * 100;
mbInstalledMemory = (int)installedMemory / 1048576; mbUsage = (int)p.PrivateMemorySize64 / 1048576;
Cheers!
nosebleed*
ReplyDelete