How to find out size of session in ASP.NET from web application?

If you’re trying to get the size of Session during runtime rather than in debug tracing, you might want to try something like this:

long totalSessionBytes = 0;
BinaryFormatter b = new BinaryFormatter();
MemoryStream m;
foreach(var obj in Session) 
{
  m = new MemoryStream();
  b.Serialize(m, obj);
  totalSessionBytes += m.Length;
}

(Inspired by http://www.codeproject.com/KB/session/exploresessionandcache.aspx)

Leave a Comment