如何获取 HttpRuntime.Cache 中对象的大小?
我目前在 ASP.NET HttpRuntime.Cache 中存储许多不同类型的对象,我想知道是否有办法计算出每个对象有多大?
I am currently storing many different types of objects in the ASP.NET HttpRuntime.Cache and I was wondering if there is a way to figure out how big each object is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这些问题:
获取字段的大小C# 的字节
找出 .net 对象的大小
特别是,在这些问题中寻找乔恩·斯基特的答案。 他们会告诉您为什么该数字不准确。
至于进行估算,除非您的对象需要满足某些标准,否则无法做到这一点。
例如,如果缓存中有许多对象,共享对某些公共对象实例的引用,则序列化缓存中的其中一个对象也会序列化这些公共对象的副本,从而夸大结果。
Look at these questions:
Getting the size of a field in bytes with C#
Find out the size of a .net object
In particular, look for Jon Skeet's answer in those questions. They will tell you why the number won't be accurate.
As for getting an estimate, there is no way to do that unless there are certain criteria to be met for your object.
For instance, if you have many objects in the cache, sharing references to some common object instances, serializing out one of those objects in the cache will serialize out a copy of those common objects as well, inflating the results.
您可以做的一件事是将对象序列化到磁盘上的文件。 这应该会给你一个想法。
ASP Alliance 的缓存管理器也可能有帮助
One thing you can do is serialize the object to a file on disk. That should give you an idea.
This cache manager from ASP Alliance might help as well