How to measure the accruate memory usage for a code or process?

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 ?


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?


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


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.

链接地址: http://www.djcxy.com/p/80168.html

上一篇: Solaris上运行时进程的当前内存使用情况

下一篇: 如何衡量代码或过程的累积内存使用情况?