cameron <ca****************@appdepot.com> wrote:
I need to get the size of an objet in memory. I have tried:
System.IO.MemoryStream m = new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter b = new
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
b.Serialize(m, Obj);
double size = Convert.ToDouble(m.Length);
but not everything is serializable. The thing is I want to be able to
take an arbitarty object, (since everything I am attempting to size is
comming out of the cache), and get the size of it. I am wondering if it
not possible to treat it like a stream or a byte array or something like
that so that it is easy to get the size. I don't need any attributes off
of the object, I just need the size. Anyone have any ideas?
"Size" can be either misleading or meaningless when you consider
reference types. For instance:
public class Foo
{
string x;
public Foo (string x)
{
this.x=x;
}
}
Foo f1 = new Foo ("hello");
Foo f2 = new Foo ("hello");
Now, what is the size of f1 and f2? Combined, it should be double the
size of the Foo class instance itself (probably 12 bytes - an object
header of 8 bytes plus a reference) and the size of the string (16 or
20 bytes, at a guess) - but the size of each of them individually would
be the size of the Foo class instance itself plus the string - so the
whole is greater than the sum of the parts... but if you ignore the
string itself, it becomes meaningless in the other way.
So, what do you *really* mean, and what do you *really* want to try to
measure?
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too