Reputation: 119
I'm trying to measure the memory usage for a code and process using Process.privateBytes and Process.workingSet but if i run the application several time, each time i have different values ?
What is the accurate way to have ~same values each times ?
Upvotes: 0
Views: 454
Reputation: 12195
For accurate measurement of memory consumption you may use a professional memory profiler. However, the best ones such as JetBrains dotMemory and Redgate ANTS are not free.
Upvotes: 0
Reputation: 2508
If you need to measure memory usage from code (as you mentioned in a comment) use JetBrains dotMemory Unit framework (it's free), it's designed exactly for such tasks.
var snapshot1 = dotMemoryApi.GetSnapshot();
foo.Bar(); // run your code
var allocatedSize = dotMemoryApi.GetDifference(snapshot1).GetNewObjects().SizeInBytes;
It is designed to be used with unit tests easily, but also can be used with any application. Current EAP version contains standalone launcher for that purpose. Ask me if you need a help with that framework.
There is online doc for previous version https://www.jetbrains.com/dotmemory/unit/help/Introduction.html
Upvotes: 1
Reputation:
Process.privateBytes is an Approximation and unless the code in question and in the process has nothing but a Thread.Sleep() for all the threads it spawns, the values are bound to vary(dependening on memory consumed + GC)
Also refer What is private bytes, virtual bytes, working set?
Upvotes: 1