Estimating / calculating session memory usage

I would like to estimate the amount of memory used by each session on my server in my ASP.NET web application. A few key questions:

  • How much memory is allocated only for each instance of the session?
  • Is the memory usage of each variable equal to its address space (e.g. 32 bits for Int32)?
  • What about variables with a variable address space (like String, Array [] s)?
  • What about instances of special objects (like MyCustomObject, which contains various other things)?
  • Is there anything added for each variable (for example, the address of the Int32 variable to bind to the instance of the session), adding to the service information for the variable?

Thank you for your help in figuring out how I can accurately predict how much memory each session will consume. Thanks!

+4
source share
5 answers

The HttpSessionStateContainer class has ten local variables, so this is approximately 40 bytes, plus 8 bytes for the object's overhead. It has a session identifier string and a collection of items, so it's about 50 bytes when the collection of items is empty. It has a few more links, but I believe that these are links to objects that are common to all objects in the session. So, all that would do about 100 bytes per session object.

If you put an Int32 value type in a collection of session items, it should be boxed. With an overhead of 8 bytes per object, it reaches 12 bytes, but due to limitations in the memory manager, it cannot allocate less than 16 bytes for an object. With four bytes for an object reference, Int32 needs 20 bytes.

If you put a reference type in a collection of elements, you only save the link, so that’s just four bytes. If it is a literal string, it is already created, so it will not use more memory. The created string will use (8 + 8 + 2 * Length) bytes.

An array of value types will use (Length * sizeof (type)) plus a few bytes. An array of reference types will use (Length * 4) plus a few more bytes for the links, and each object is allocated separately.

A user object uses approximately the sum of the size of its members, plus some additional additions in some cases, as well as 8 bytes of object overhead. An object containing Int32 and Boolean (= 5 bytes) will be complemented by 8 bytes, as well as 8-bit overhead.

So, if you put a string with 20 characters and three integers in the session object that will use approximately (100 + (8 + 8 + 20 * 2) + 3 * (20)) = 216 bytes. (However, a collection of session items will likely allocate a capacity of 16 items, so there are 64 bytes of which you are using 16, so the size will be 264 bytes.)

(All sizes are indicated on a 32-bit system. On a 64-bit system, each reference is 8 bytes instead of 4 bytes.)

+3
source

.NET Memory Profiler is your friend:

http://memprofiler.com/

You can download a trial version for free and run it. Although these things sometimes become difficult to install and run, it was surprisingly simple for me to connect to a working web server and check all the objects that it stores in memory.

+1
source

You can probably use some of them using Performance Counters and Custom Performance Counters . I have never tested them using ASP.NET, but otherwise they are very good for measuring performance.

0
source

This old article from Microsoft, containing performance recommendations for ASP (not ASP.NET), states that each session has an overhead of 10 kilobytes. I don’t even know if this is applicable for ASP.NET, but, of course, it is much more than 100 bytes, as Guffa says.

0
source

Planning for a large-scale application has some other things that you probably need to consider other than rough memory.

It depends on the session state provider you selected, and by default, sessions in progress are probably not.

If the Out-Of-Process session is stored (which may be preferable for scalable applications), the image will be completely different and depends on how the session objects are serialized and saved.

There will be no linear RAM consumption in SQL Session Storage.

I would recommend integration testing with a non-standard taste of the session state provider from the very beginning for a large-scale application.

0
source

All Articles