English 中文(简体)
Can I get the size of a Session object in bytes in c#?
原标题:

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 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;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签