Tuesday, August 31, 2010

Size of Asp.Net Session Object

Since the past couple of days, I've been trying to determine a way to find out the size of a session object. Although this does not give an accurate picture of the size, it gives a decently rough estimate.


long totalSessionBytes;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream m = new System.IO.MemoryStream();
b.Serialize(m, UserData);
totalSessionBytes = m.Length;


Of course, there are some profilers that can be used such as the following:
  • CLR Profiler
  • Eqatec
However, I think for my current purpose, the above code is good.