0

I run a code multiple time and i measure in the end of it the PeakPagedMemorySize64 , PeakWorkingSet64 and PeakVirtualMemorySize64 using the Process class .

But in each time i get different value for the same code

PeakPagedMemorySize64 112758784 PeakVirtualMemorySize64 332701696   PeakWorkingSet64 143835136
PeakPagedMemorySize64 113696768 PeakVirtualMemorySize64 332636160   PeakWorkingSet64 144642048
PeakPagedMemorySize64 113528832 PeakVirtualMemorySize64 332701696   PeakWorkingSet64 144547840

why I have this different values instead of getting the same value each time? And how can I optimize it to minimize this variation?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Salim
  • 29
  • 3
  • Always go through the documentation first:- https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/garbage_collect.html – Ankit Aug 11 '15 at 15:55
  • @Ankit I'm not talking about Heap Program but the total of memory used means working set and virtual memory – Salim Aug 11 '15 at 17:28
  • @ankit is it more clear now after editing the question ? – Salim Aug 12 '15 at 14:54

1 Answers1

1

"Working set" is the amount of virtual memory which fits into physical RAM. The amount of physical RAM available depends on other programs as well, so this value changes over time. If the "working set" value changes due to other programs, so will the peak value.

In fact, you should be happy if this value is high: this means that all your data is in RAM and RAM is fast. If this value is low, some data has been swapped to disk and disk is very slow.

The question is: why do you want to measure this value? If you're looking for a memory leak, measure private bytes instead.

Since you have tagged this , the amount of private bytes heavily depends on the implementation of the .NET garbage collector. It may request more or less memory depending on garbage collections which ran or did not run previously and how much physical RAM was available at the time .NET requested additional memory the last time.

To see those internals, I recommend using and . You can then see the amount of free memory of the .NET garbage collector.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • "Working set" is the amount of virtual memory which fits into physical RAM, but it's for the current process how much phyisical memory he is using ? – Salim Aug 12 '15 at 14:59
  • the same variation is also for private bytes , i'm not trying to find a leak but just trying to know how much memory the code or process cosumme, but if this value is different each time, means it's not accurate – Salim Aug 12 '15 at 15:01
  • evenif their is no garbage collection, i get variations. – Salim Aug 12 '15 at 15:03
  • How did you check that there are no garbage collections? – Thomas Weller Aug 12 '15 at 15:05
  • display GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2) – Salim Aug 12 '15 at 15:08
  • 1
    .NET may allocate more memory if there was more free memory available the last time memory needed to be allocated. – Thomas Weller Aug 12 '15 at 15:10
  • Ah really can i get the sources to find this info about .net allocation ? – Salim Aug 12 '15 at 15:11
  • Ok, now I see what you want... I think I read about it in http://www.amazon.de/dp/0321578899/ Unfortunately I'm on vacation in the moment and I don't have that book with me. Perhaps I could write an example program demonstrating it by setting breakpoints in `VirtualAlloc` – Thomas Weller Aug 12 '15 at 15:19
  • What is your system specs? 64 bit OS, <=12 GB RAM, 8 cores? – Thomas Weller Aug 12 '15 at 16:27
  • Yes, that's it i have the book and i can't find the part which it say "NET may allocate more memory if there was more free memory available" – Salim Aug 12 '15 at 17:31
  • i need just to understand why we have the interval if it's the same program for a working set and virtual memory, The paged is okey so we will have less page file if we have lot of free memory space , but what about the other metrics ? – Salim Aug 13 '15 at 09:31
  • Please hold on. I finished part one, a program which consumes much physical RAM. I'm working on a .NET program allocating RAM under good and bad circumstances. Third are the debugging steps to proove what's going on. It'll take a while... – Thomas Weller Aug 13 '15 at 17:05