Profile Memory Usage of Session State ASP.Net

I’m trying to figure out the size of a particular session state. On one of our heavy pages (lots of data in a table) it gets progressively slower. The issue is resolved by logging out of the system.

I’ve profiled the page looking for JavaScript memory leaks, but I didn’t find anything. My next plan of attack is too look at ViewState and Session State. ViewState will be simple, but Session State poses a challenge.

Does anyone know of any tricks or tools that would help figure out the size of Session State?

EDIT

The session state is InProc.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Measure it:

int totalBytes;
var formatter = new BinaryFormatter();
for(int i = 0; i < Session.Count; i++)
{
    using (var stream = new MemoryStream())
    {
        formatter.Serialize(stream, Session[i]);
        stream.Flush();
        totalBytes += stream.Length;
    }
}

Also I believe that if you enable tracing it will show you some details about the session (not sure about this, never tried it myself).

Method 2

Some have mentioned ASP.NET tracing but I didn’t have much luck with it myself. I could view trace information but the session section was never populated.

However, here’s a useful article from CodeProject that uses http handlers to view the current session (and cache).

There are two issues with respect to this question:

  1. It won’t present storage space when session state is InProc

    When Session State is running InProc
    (In Process) the actual objects that
    form the content are not stored in the
    collection, only reference to the
    objects. The figures for the size
    taken up by these objects “in” session
    state would be misleading under these
    circumstances.

  2. It uses BinaryFormatter which “gives only a very rough approximation” of the size of the session data. It’s only an approximation as ASP.NET “uses an optimized internal formatter for basic types such as int, string, bool, etc”

That said, I’ve found it useful and I thought it was worth sharing. It may be worth pushing the session state out of process for profiling size.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x