Donate

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:
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:
mbInstalledMemory = (int)installedMemory / 1048576;  
mbUsage = (int)p.PrivateMemorySize64 / 1048576;
Then you can apply the percentage formula.
Cheers!

Comments

Post a Comment

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.