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
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).
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:
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.
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.