Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi guys,


please let me know what is the size of the class in c#.

for e.g

a) what is the size of class info ?
class info
{
    protected string name;
    protected string age;
    protected string emailid;
   
    public void display()
    { 
Anything u want to display....
}
}

b)what is the size of this empty class ?
class studentinfo
{ } 

please explain the size of each datatype & function in brief.

Thanks in advance.

Aditya.
Posted
Updated 5-Apr-11 22:52pm
v2

You may have a conceptual problem here - the size of that class is not really supposed to be exposed to you, the developer in this case.

The underlying mechanisms used to store it are compiler details - also the compiler is optimising - which means that it is perfectly at liberty to change the sizes of certain objects if it provides a performance upgrade.

In general if an data member is an object type it will add 32/64 bits to the size of the class (to handle the address to it).

If the data member is a value type, it will add the size of the value type to the class.

Again - don't rely on this data - the compiler is at liberty to choose how to store the class as long as it adheres to the standard.

Sizes of classes is a C++ concept, and even then is flaky - sizeof() in C++ can also measure the size of the virtual function table pointer IF the compiler in question implements one in a specific way.

If you really need to know more about the size of this object internally, can you let us know why? This may make things clearer :)
 
Share this answer
 
Comments
Olivier Levrey 6-Apr-11 5:44am    
My 5 for this comprehensive answer.
[no name] 6-Apr-11 9:40am    
thnx Davekerr.

I m curious to know how memory is allocated to each data member ?
how much time compiler will take to do all such stuff ?
can we enhance the speed of compiler ?
Dave Kerr 6-Apr-11 10:12am    
Hi,

OK, to each data member that is an object type we get 32 bits allocated (on a 32 bit system) or 64 bits allocated (on a 64 bit system). For each value type, we get the size of the value type. Therefore:

class something
{
string str1;
string str2;
string str3;
Int64 int1;
}

will give us 3 x 4 bytes for the strings (internally stored as a pointer to the string, i.e a memory address) and 64 bits (8 bytes) for the Int64. So we can roughly estimate this class will take up 20 bytes on the heap.

However, take a look at the class below:

class parent
{
someclass child1;
someclass child2;
}

In this case the someclass members are objects, so although each on takes up ~ 20 bytes the parent class needs only 8 bytes - enough to store two pointers.
creating an instance of parent takes up at least 8 bytes - even if the someclass members are not initialised.

in terms of time taken, the clock cycles used to allocate the memory are very few - allocating and freeing memory is generally a rapid operation.

we ourselves cannot enhance the speed of the compiler (it is very fast anyway) but you can certainly enhance the speed of the program by following some guidelines:

* create child classes only when needed. in the case of 'parent' above, don't created child1 and child2 in the constructor - create them only when required.
* make (very) small classes structures. if we have the class 'vector'
class vertex
{
int x;
int y;
int z;
}
it is so small (~12 bytes) that the saving in storing as a pointer (12-4 = 8 bytes) is very small - if we're doing lots of work with vertex objects, manipulating, copying, passing them around, we will in general be faster defining them as 'structures' so that they are copied by value (i found this out the hard way with a c# openGL port).

also look into 'boxing' and 'unboxing' - there is an overhead in converting a value to an object and vice versa, i.e.

function(object o)
{
...
}

int i = 0;
function(i);

in this case we have to 'box' i (which is a value) into an object to be passed to function.
[no name] 6-Apr-11 11:44am    
thnx for ur excellent reply.

Is there any way i can calculate the clock cycles ? so that i can analyse it more briefly.
Dave Kerr 6-Apr-11 11:55am    
For sure, although you'll need the right version of visual studio (Ultimate or Premium).

The task you are looking at is known as 'profiling', which involves running the application whilst visual studio (or another profiling tool) gathers low level data about how the CPU is being hit.

It's a complicated area, so the best I can do is suggest looking at:

http://msdn.microsoft.com/en-us/library/ms182372.aspx

and taking it from there :) Good luck!
I think the best answer was given by DaveKerr. I will just say a word about marshaling.

It is possible to compute the size of a class if you want to marshal it to native code. You can use the Marshal.SizeOf method to get the size of a "marshalable" type. This type and data members of that type should eventualy be declared with some specific attributes: MarshalAs, StructLayout, ...

There are lots of tutorials about marshaling if you need to learn more about that.
 
Share this answer
 
Comments
[no name] 6-Apr-11 9:47am    
thnx man
Sergey Alexandrovich Kryukov 6-Apr-11 14:52pm    
Yes, my 5.
--SA
Olivier Levrey 7-Apr-11 3:26am    
Thank you SA.
Nuri Ismail 7-Apr-11 12:02pm    
My 5.
Olivier Levrey 8-Apr-11 3:44am    
Thank you Nuri.
The .NET runtime does not expose the mechanism of calculating the size of a reference type. Normally you wouldn't care of it. You can play with summing up the sizeof each member element of the class. Note that you cannot use sizeof operator for reference type as it will return the size of the pointer but not the actual amount of memory used by the object.
 
Share this answer
 
v2
Comments
Olivier Levrey 6-Apr-11 5:48am    
Your answer is good although it is not completely true. A type can be both a reference type and marshalable. Therefore, its size can be calculated with Marshal.SizeOf
From MSDN about Marshal.SizeOf method: This method accepts an instance of a structure, which can be a reference type or a boxed value type.
I voted 4.
Sergey Alexandrovich Kryukov 6-Apr-11 14:52pm    
I agree with you, Olivier, about the issue being more complex. I need to get to your Answer...
As to this one... OK 4 would be fair, I'll join you.
--SA
A class does not have any size in terms of memory usage.
Only when you create an instance will some memory be allocated to this instance.

Also, its not possible to measure an exact size used by an instance.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Apr-11 14:50pm    
This is basically correct, but is simply means the size should be defined more accurately. There is a size of the references (of IntPtr), size of by-value members, etc.
--SA
The size of a class is the total size of all members of that class. You really need to buy a basic c# book and read up about DataTypes and their sizes, this is really elementary stuff you should know before anything else.
 
Share this answer
 
Comments
Olivier Levrey 6-Apr-11 5:49am    
Better answers were given there. This is not so simple...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900