[Mono-list] Objects sizes and the GC.

Rafael Ferreira Rafael.Ferreira@apollogrp.edu
Fri, 6 Aug 2004 09:37:24 -0700


Hey everyone, 

I'm working on an application using mono/.net that implements various
in-memeory cache algorithms for very large objects and one of the issues I'm
trying to address is when to swap cached objects out of memory.
Historically, this would be done in 3 different ways: by setting a hard
limit on the number of objects cached and swapping out after reaching the
limit, by querying the memory utilization every time you would something
like malloc() and finally by using some algorithm that would attempt to
predict the need for swapping based upon the hit/miss ratio. 


Now, I'm building something that utilizes both the hit/miss ratio but that
would be smart enough to not rely on the OS to swap stuff to disk and that's
where I'm having issues. To the best of my knowledge, the only interface
into the inner workings on the GC is via System.GC (GC.GetTotalMemory) and
that gives me basically no access to how much memory an object is taking at
any given time. I do understand that this is done that way for a reason,
since memory footprint in a GC based environment is not an exact science,
but I believe that there must be another facility that I can use to at least
get an approximate value of an object memory utilization. 

Also, I've heard of Marshal.SizeOf() but according to the documentation that
only works in unmanaged objects, and I think it would be too costly of a
process to marshal my objects just so I can query it's memory footprint.
Speed is very, very important in my app. 

Let me know if you guys can help me, 

- raf