0

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 ?

miloud
  • 109
  • 1
  • 1
  • 6
  • If the process is not that big, you could make yourself an assumption on how much memory the process needs (as you can look up the size of different datatypes). What are you feeding the process for data? Besides that, Visual Studio has a monitor which can be used to monitor your application while debugging. – Martin Beentjes Aug 03 '15 at 08:59
  • @MartinBeentjes but if i call any other external code like using reflection, how i can know all datatypes created on runtime to know what are the datatypes used ? – miloud Aug 03 '15 at 09:29

3 Answers3

1

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?

Community
  • 1
  • 1
1

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

Ed Pavlov
  • 2,353
  • 2
  • 19
  • 25
0

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.

Ed Pavlov
  • 2,353
  • 2
  • 19
  • 25
Sebastian Negraszus
  • 11,915
  • 7
  • 43
  • 70
  • I want to use it programaticaly, i need to run the process or a code and siplay in the console the memory usage ? so do u have any idea on how to do it ? – miloud Aug 03 '15 at 09:28
  • @miloud In .NET, the memory is managed by the GC and out of your control. You may make an educated guess, but a .NET application cannot really accurately measure its own memory consumption. – Sebastian Negraszus Aug 03 '15 at 09:34
  • so if it's not accurately their is any way to measure it in interval like from [XXXko,YYYKo], i used ETW to catch the GC signal and memory free by GC using perfMon and also same thing can't have ~same value each time if u run the same process – miloud Aug 03 '15 at 09:56