Domanda

OK, all you ASP.NET Experts: I have used reflector to look into ASP.NET Cache implementation (which sits on HttpRuntime.Cache and HttpContext.Current.Cache) uses a Hashtable internally to keep the cache.

However, the data gets stored in unmanaged memory. This is very strange since I could not see anywhere data getting stored in unmanaged memory. However, writing a very simple web application that inserts a chunk of byte array into cache, we can see this:

enter image description here

  • Private Bytes: 460MB
  • Bytes in all heaps: 150MB

=>

Managed Memory: 150 MB

Unmanaged Memory: 310 MB

So basically I am calling the application many times (each increase is 1000x requests each putting 64KB empty buffer byte[] into cache). So the one that has grown the most is private bytes (total memory) instead of bytes in all heaps (managed memory). However I am expecting managed memory to grow in line with total memory since I am adding objects to the managed heap using Hashtable.

Can you please explain this behaviour?


UPDATE

As Simon said, the bytes in all heaps value only changes after a garbage collection - I changed the code to induce garbage collection and it update the counters. Increase in Gen 2 Heap memory is EXACTLY the same as the amount of memory added. However, unmanaged memory is still much higher. In this example, Heap 2 was only 96MB while total memory 231 MB.

enter image description here

È stato utile?

Soluzione

The # Bytes in all Heaps is only updated when the garbage collection is executed, while the Private Bytes is available at much faster update rate. (I'm not sure where that number comes from, internally, and how often it's updated.)

The amount of Private Bytes increases just after 17:42:45. This amount does seem to match the value jump of # Bytes in all Heaps at about 17:43:10. It looks like it took 20-25 seconds before any garbage collection was done and updated the # Bytes in all Heapscounter.

It's hard to work out how memory allocations work from a few minutes worth of performance counters presented in a screenshot. ;) Keep running your test and see how your expectations work out over a longer time period.

TL;DR: The amount of managed bytes should correlate with private bytes, but the managed counter will only update during a garbage collection.


Small note from the OP: As this response says, the lagging in the memory can be fully explained by lagging GC. The fact that unmanaged memory also rises was not my question. So thanks @Simon.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top