How can I get the size of an object in the HttpRuntime.Cache?

情到浓时终转凉″ 提交于 2019-12-10 14:10:45

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/326606/how-can-i-get-the-size-of-an-object-in-the-httpruntime-cache

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