How to find out size of ASP.NET session, when there are non-serializable objects in it?

旧时模样 提交于 2020-01-15 02:47:48

问题


I have a feeling that I'm putting rather much data in my ASP.NET session, but I've no idea how much and if I should be concerned. I found a similar question, but that relies on serializing objects and checking their serialized size. In my case the majority of data in the session is in objects from another library which doesn't have its classes marked as "Serializable". (I know that this restricts me to using InProc session state provider, but that's another issue). Does anyone have an idea on how to traverse the object graph and find out its size?

Added: OK, one way would be a manual traversal of the object graph and usage of Marshal.SizeOf() method. But that's a lot of writing to get that working. Is there perhaps a simpler way of achieving the same effect? I'm not aiming for byte precision, I'm interested in the order of magnitude (kilobytes, megabytes, tens of megabytes...)


回答1:


For testing, you could put together a stub Custom Session provider, implementing the SessionStateStoreProviderBase abstract class. I would write backing fields that stash everything in WebCache (so that you have session data managed), and eventually generate a statistic using the Marshal.SizeOf() method when the SetAndReleaseItemExclusive method is called.

        public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
        {

        double MemSize = 0;
        foreach (object sessObj in item.Items)
        {
            MemSize += Marshal.SizeOf(sessObj);
        }

}

Consult this question for more information on getting field size: Getting the size of a field in bytes with C#




回答2:


can't you generate a heap dump and find the size of the session from that. in java land i can dump the heap then open it up in mat, find the session object and find out the size of the subgraph.




回答3:


You can probably store the session state information in the database and check the size, but I am not sure if there is any tool out there that can let you view and traverse the object graph.

If possible, check your design one more time to see if you can minimize the information in the session.



来源:https://stackoverflow.com/questions/751710/how-to-find-out-size-of-asp-net-session-when-there-are-non-serializable-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!