473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Size of an instance in memory

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!
Nov 16 '05 #1
5 2626
Mariano Drago wrote:
[...]
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.
Nov 16 '05 #2
Yes, your point sounds logical.
And what about the size of the class / instance?

Thanks!
Nov 16 '05 #3
Mariano Drago wrote:
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.
Nov 16 '05 #4

"Mariano Drago" <md****@softhome.net> wrote in message
news:uo****************@TK2MSFTNGP09.phx.gbl...
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!

Nov 16 '05 #5
Thanks for your answer!

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> escribió en el
mensaje news:Op**************@TK2MSFTNGP09.phx.gbl...

"Mariano Drago" <md****@softhome.net> wrote in message
news:uo****************@TK2MSFTNGP09.phx.gbl...
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!


Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: cameron | last post by:
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.BinaryFormatter b = new...
17
by: TheMadHatter | last post by:
yello, Quick Q: With regards to ram use, there really isnt any memory savings if I use type bool, vs int....... is there? If I remmeber correctly from a microcontroller course, the smallest...
96
by: subramanian | last post by:
I have been thinking that all pointers(to any obejct) have the same size. The size of a pointer is the size of an int. This is beause a memory location is addressed by an int. Is that right/wrong?
8
by: filox | last post by:
is there a way to find out the size of an object in Python? e.g., how could i get the size of a list or a tuple? -- You're never too young to have a Vietnam flashback
2
by: D. Susman | last post by:
Hi, I know that this issue is indeed strictly operating system dependent but I am just curious: I have a five dimensional array, whose size sums to 68 MB (almost). This array is contained by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.