Getting the size (ie. in bytes) of an in-memory object

  • Thread starter Thread starter Phil Jones
  • Start date Start date
P

Phil Jones

Is there a way to determine the size (number of bytes) of an object?

I figure this can be done by serializing the object to disk and measuring
the file size - but I definately don't want to do this for performance
reasons. I'm hoping there is some Framework class that dishes up the
in-memory size of an object.

Is there???

Thanks everyone....

Phil (Aotearoa :: NZ)
 
Phil Jones said:
Is there a way to determine the size (number of bytes) of an object?

I figure this can be done by serializing the object to disk and measuring
the file size - but I definately don't want to do this for performance
reasons. I'm hoping there is some Framework class that dishes up the
in-memory size of an object.

You really need to define exactly what you mean by the in-memory size
of an object. Most objects refer to other objects, for instance. If you
had two objects of class Foo, declared below, but they both referred to
the same large string, what would the size of each object be? Just the
size needed to store the string reference but not the string itself, or
the size needed to store everything?

class Foo
{
string bar;
}
 
I'm wanting to manage the size of a hash-table, and do certain things when
the contents (that is the cumulative size of the object's contained within
it) reaches a given threshold.

So defining the question more accurately within your guidelines, I'm looking
for the size to store everything.

Thanks....
 
Phil Jones said:
I'm wanting to manage the size of a hash-table, and do certain things when
the contents (that is the cumulative size of the object's contained within
it) reaches a given threshold.

So defining the question more accurately within your guidelines, I'm looking
for the size to store everything.

Then you need to more carefully define "everything" otherwise you'll
end up with "size of objects outside hashtable + size of objects inside
hashtable > total size" for instance (if they share objects, etc).

I suspect you'll find you basically can't do this though. Even
serialising would only give you some kind of guesstimate.
 
Cool...thanks for that.

As I'll be wanting at look for the size of reference types too this won't
really work - so I guess it's like Jon suggested above - this kind of thing
really isn't possible.

Cheers everyone.
 
Back
Top