determining size of object

  • Thread starter Thread starter Anthony Sox
  • Start date Start date
A

Anthony Sox

Hi all

is there as way i can tell the amount of memory my object takes. for example
if i have a collection of employee object say of 20, how much memory do they
take e.g. 500kb.

thank you.
 
Anthony Sox said:
is there as way i can tell the amount of memory my object takes. for
example
if i have a collection of employee object say of 20, how much memory do
they
take e.g. 500kb.

No (there is no method to do that in the .NET Framework).
 
Hi all

is there as way i can tell the amount of memory my object takes. for example
if i have a collection of employee object say of 20, how much memory do they
take e.g. 500kb.

thank you.

There isn't. But you can try the memory usage with testing with small
increments by putting more objects. Then you'll get a formula for
yourself, that may make easier to estimate memory usage at the end of
software.
 
Anthony Sox said:
Hi all

is there as way i can tell the amount of memory my object takes. for
example
if i have a collection of employee object say of 20, how much memory do
they
take e.g. 500kb.

..NET deliberately makes it extremely difficult to determine how much memory
an object actually consumes because the information is architectural and you
can't go messing about with the .NET architecture.

That said, there are at least two ways out of the near predicament.

1. Spend a few weeks mucking about with the CopyMemory API, allocating
objects to managed and unmanaged memory, interop object marshalling and
pointer to structure manipulations... and getting nowhere fast.

2. State the general structure of the objects and the purpose of needing to
know how much memory each one object consumes.

If you take option 2 then it can be determined if an approximation might be
sufficient, and if that is the case then there are any number of ways to
skin your particular cat.
 
Hi Anthony,

For simply types you can use Marshal.SizeOf, but for any complex types you
will need to use a managed profiler.
 
Back
Top