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

2

5

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?

Vocative answered 28/11, 2008 at 20:30 Comment(0)
N
5

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.

Nimwegen answered 28/11, 2008 at 21:14 Comment(2)
Thanks - it sounds like there isn't a completely accurate way, but it might be good enough for what I'm trying to do.Vocative
@JonTackabury, what method did you go with?Rummel
K
1

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

Koonce answered 28/11, 2008 at 21:8 Comment(1)
I tried the ASP Alliance tool and I get "Data could not be serialized to Xml" for most of the objects I'm interested in... Does anyone have any sample code to take arbitrary stuff from HttpRuntime.Cache and serialize it?Sparks

© 2022 - 2024 — McMap. All rights reserved.