当 ASP.NET 会话中有不可序列化的对象时,如何确定该会话的大小?
我有一种感觉,我在 ASP.NET 会话中放入了相当多的数据,但我不知道有多少数据以及是否应该担心。 我发现了类似问题,但这依赖于序列化对象并检查其序列化大小。 就我而言,会话中的大部分数据位于另一个库的对象中,该库的类没有标记为“可序列化”。 (我知道这限制我使用 InProc 会话状态提供程序,但这是另一个问题)。 有谁知道如何遍历对象图并找出其大小?
添加:好的,一种方法是手动遍历对象图并使用 Marshal.SizeOf() 方法。 但为了让它发挥作用,需要写很多文章。 是否有更简单的方法可以达到相同的效果? 我的目标不是字节精度,我感兴趣的是数量级(千字节,兆字节,几十兆字节......)
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...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了进行测试,您可以组合一个存根自定义会话提供程序,实现 SessionStateStoreProviderBase 抽象类。 我将编写将所有内容存储在 WebCache 中的支持字段(以便管理会话数据),并最终在调用 SetAndReleaseItemExclusive 方法时使用 Marshal.SizeOf() 方法生成统计信息。
有关获取字段大小的更多信息,请参阅此问题:
使用 C# 获取字段的大小(以字节为单位)
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.
Consult this question for more information on getting field size:
Getting the size of a field in bytes with C#
您不能生成堆转储并从中找到会话的大小吗? 在java land中,我可以转储堆,然后在 mat 中打开它,找到会话对象并找到出子图的大小。
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.
您可能可以将会话状态信息存储在数据库中并检查大小,但我不确定是否有任何工具可以让您查看和遍历对象图。
如果可能的话,再检查一次您的设计,看看是否可以最大限度地减少会话中的信息。
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.