开发者

Can I get the size of a Session object in bytes in c#?

开发者 https://www.devze.com 2022-12-11 05:47 出处:网络
Is it possible to get the size(in bytes) of a Session object after storing something such as a datatable inside it?

Is it possible to get the size(in bytes) of a Session object after storing something such as a datatable inside it?

I want to get the size of a particular Session object, such as Session["table1"], not 开发者_JAVA技巧the whole Session collection, so the other question, while helpful, is not quite a duplicate.


You can use marshalling to create a copy of the object, that would give you an approximate number on how much memory it uses.

But, as always it's impossible to give an exact figure of the memory usage. A DataTable object is not a single solid piece of memory that you can measure. It contains a lot of objects and they have references between them, and there may be several references to the same object which means that there isn't one copy of the object for each reference to it. Each DataRow for example has a reference to the table that it belongs to, but that of course doesn't mean that each row has a complete copy of the entire table.


You could use reflection, see this article.

You might also want to consider having a look at some Memory Performance Counters or perhaps profiling your application with a tool such as DotTrace or the CLR Profiler.


Maybe you can use external tools like CLR Profiler or VSTS Profiler to check it.


This is taken almost line-for-line from the "duplicate question" from the first comment in the question.

int totalSessionBytes;
BinaryFormatter b = new BinaryFormatter();
MemoryStream m;
b.Serialize(m, Session["table1"]);
totalSessionBytes = m.Length;
0

精彩评论

暂无评论...
验证码 换一张
取 消