determining size of object

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.
 
H

Herfried K. Wagner [MVP]

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).
 
K

kimiraikkonen

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.
 
G

Guru

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.
 
B

Bill McCarthy

Hi Anthony,

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top