473,398 Members | 2,212 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,398 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 1266
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

7
by: Bart Torbert | last post by:
Hello, I am starting to examine using SQLServer instead of Oracle. I went through the SQLServer import utility to copy tables from Oracle into SQLServer. I then checked the size of the...
36
by: xixi | last post by:
hi, we are using db2 udb v8.1 on windows, i try to use the configuration advisor to get recommendation on the parameter setting, at first, i give the target memory for DB2 is 80% of the physical...
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...
6
by: Tarun | last post by:
Hi coders, How can check the total RAM size and the number of RAM modules and their respective sizes using C#? Regards, Tarun
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...
30
by: Angel Tsankov | last post by:
Hello! Does the C++ standard define what happens when the size argument of void* operator new(size_t size) cannot represent the total number of bytes to be allocated? For example: struct S {...
2
by: shorti | last post by:
Hello, I am running DB2 V8.2 on AIX. I am looking for a way to get the memory size of a specific db2 agent. I have found the db2mtrk tool that gives me a list of all the applications and heap...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.