Size of an instance in memory

  • Thread starter Thread starter Mariano Drago
  • Start date Start date
M

Mariano Drago

Hi there.
How can i know how much memory is an instance taking, devided by internal
instances. For examples lets say i have 2 classes, C1 and C2.
C1 have an internal instance of C2
How can i know the size of:
C1 class
C2 class
C1 instance

Another question. If C1 has an instance method (1MB code), and i create 10
instances of C1, im using 10MB of ram or the code of the methods are shared
through the instances?

Thanks in advance!
 
Mariano said:
[...]
Another question. If C1 has an instance method (1MB code), and i create 10
instances of C1, im using 10MB of ram or the code of the methods are shared
through the instances?

No! All methods, whether instance or static, will only appear in memory
*once*. Why would you need to have a duplicate for every instance?
That'd be awfully inefficient.

An instance method has an invisible 'this' parameter, which refers to
the instance you are calling the method upon. Consider the following:

MyType a = new MyType();
MyType b = new MyType();
a.DoSomething();
b.DoSomething();

In the above, the *same* DoSomething method is called twice, but with a
different parameter each time.
 
Yes, your point sounds logical.
And what about the size of the class / instance?

Thanks!
 
Mariano said:
Yes, your point sounds logical.
And what about the size of the class / instance?

Maybe someone could help if you gave details about why you want to know,
i.e. where you'll use this info.
 
Mariano Drago said:
Yes, your point sounds logical.
And what about the size of the class / instance?

You can only retrieve the *unmanaged* size of an instance or type of a
reference type. To do this you would use the SizeOf method on
System.Runtime.InteropServices.Marshal.

Note, I said unmanaged size. The unmanaged size of an object is the number
of bytes it takes up when it is marshalled. This can be anywhere from a
little under the size of the managed object to half the size or so in
theory(unicode-16 strings marshaled to an 8bit encoding). I'm pretty sure it
will always be smaller than the actual object because some bits of
information aren't marshalled but are stored in classes. Not to mention
object headers, etc which aren't considered.

You can determine the size of a valuetype by using the sizeof() operator in
unsafe code. But this works only on value types and I don't think it works
when the valuetype contains references(someone correct me if I'm wrong).
 
Thanks for your answer!

Daniel O'Connell said:
You can only retrieve the *unmanaged* size of an instance or type of a
reference type. To do this you would use the SizeOf method on
System.Runtime.InteropServices.Marshal.

Note, I said unmanaged size. The unmanaged size of an object is the number
of bytes it takes up when it is marshalled. This can be anywhere from a
little under the size of the managed object to half the size or so in
theory(unicode-16 strings marshaled to an 8bit encoding). I'm pretty sure it
will always be smaller than the actual object because some bits of
information aren't marshalled but are stored in classes. Not to mention
object headers, etc which aren't considered.

You can determine the size of a valuetype by using the sizeof() operator in
unsafe code. But this works only on value types and I don't think it works
when the valuetype contains references(someone correct me if I'm wrong).
 
Back
Top