Click here to Skip to main content
15,885,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to calculate size of class.

What I have tried:

C#
class A
{

   int a;
   float f;

  public void cal();
  public void cal2();

}


what is size of this class?
how to calculate it?

is memory allocated for function ?

is this declaration allocate a memory or

C#
A a=new A();


will allocate a memory?
Posted
Updated 2-Dec-16 3:00am
v2

1 solution

Methods take no space in a class instance (although they do take space in the EXE file).
You can get the size of the class quote easily, but ... you don;t need to, except when you talk to unmanaged code - and the value you get is probably rather confusing.
But...
C#
int size = Marshal.SizeOf(typeof(Myclass));
Will tell you.
Run on your class:
C#
[StructLayout(LayoutKind.Sequential)]
class Myclass
    {
    int a;
    float f;
    public void cal() { }
    public void cal2() { }
    }

It will give you "8" because an int is 4 bytes, and a float is 4 bytes. But it won't tell you what you think!
If I add to your class:
C#
[StructLayout(LayoutKind.Sequential)]
class Myclass
    {
    int a;
    float f;
    int[] array = new int[100000];
    public void cal() { }
    public void cal2() { }
    }
How big do you think it will give you now?
The answer is "16" - the array takes 8 bytes! How? Because the array is a reference, and like all class instances all a variable contains is a reference to the actual data - and a reference on a 64 bit system is always 8 bytes, regardless of the size of the data it references.
 
Share this answer
 
Comments
Kishor-KW 2-Dec-16 7:34am    
so there is only reference of 8 byte is created for a class. and if any other reference type declared in it then it will be add 8 in it.

class Myclass
{
int a;
float f;
char c;
public void cal() { }
public void cal2() { }
}

if my class will still occupy 8 byte ?

and if memory is not allocated for a function then how they are called ? are there reference is stored on stack ? and just reference address is fetches at the time of run. if reference is stored on stack then there will a some memory is allocated it's an address then is should be 8 byte, right?

and for each instance memory is allocated, if my class size is 8 byte and i create two instance of it then it will take 16 bytes of memory ?
OriginalGriff 2-Dec-16 7:58am    
"Memory" isn't allocated for a method because you only need one copy of it - all instances of the class use the same code.
(This is a lie-to-children, as the code you write is converted into MSIL by the compiler, which is converted to native code at run time by the JIT on a line-by-line basis, which all takes memory - but that's the advanced stuff and you really don't want to get into that yet!)

No, your class will not occupy 8 bytes if you add a char to it - it will go up from 8 to 12, because a char value occupies 2 bytes (and data is always aligned to boundaries which are a multiple of 4 for a huge number of reasons I don't want to go into just now (this is only a little textbox!).

A reference to the data is not the data itself - that's why it's called a "reference"! A reference "points at" the real data on the heap. All classes are reference types, unlike structs which are value types.
This will probably not help much:
https://www.codeproject.com/Articles/728836/Using-struct-and-class-whats-that-all-about
But it does try to explain some of what a reference is - and it's important that you know! Have a read - it's pretty simple to start with.

If your class is 8 bytes, and you create two instances of it, each instance will be a separate 8 bytes, so it will take 16 bytes to store them both, yes. (And that's another lie-to-children - there is a lot of stuff going on behind the scenes that you don't need to know yet, if ever, and that takes more memory).
CPallini 2-Dec-16 8:59am    
5.
Kishor-KW 3-Dec-16 5:33am    
lie-to-children, Thanks :) 5
OriginalGriff 3-Dec-16 5:53am    
You're welcome!
"Lies-to-children" are when the teacher doesn't tell you the whole truth - just "all you can understand at the moment". Like physics at school - they tell you the atom is the smallest particle, then the next year it's "well, that's not the whole truth, atoms are made of protons, neutrons, and electrons - but they are the smallest", then the following year quarks start to appear, and then electrons aren't really particles, and they don't really orbit, and...
"Lies-to-children" :laugh:
Teachers get quite upset when you use that phrase and prefer things like "age appropriate approximations", but that is exactly what they are: lies. ;)

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