Hi Guys,
Can anyone tell me what will be the size of the empty class ( Memory consumed ) in C# ??
If this is the case ,
class Sample
{
public static void main ( String args[])
{
}
}
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Iftikar HussainRank #423 · 5.9K reputationPosted Sep 18, 2013, 4:07 AM EDT
Please refer the below link
http://msdn.microsoft.com/en-us/library/f42z47h2.aspx
Regards,
Iftikar
Hemant SrivastavaRank #581 · 4.2K reputationPosted Sep 18, 2013, 5:55 AM EDT
http://stackoverflow.com/questions/2331889/how-to-find-the-size-of-a-class-in-c-sharp
VulpesRank #55 · 29.2K reputationPosted Sep 18, 2013, 7:31 AM EDT
In .NET, every instance of a class is allocated on the managed heap and consists of two parts - overhead and data.
The size of the overhead is 8 bytes on a 32 bit system and 16 bytes on a 64 bit system.
The size of the data part is the sum of the bytes required by the individual instance fields of the class plus any padding needed for memory alignment purposes.
Even if there are no instance fields, for technical reasons there still needs to be a way to point into the data part. The CLR therefore allocates an additional byte which, for memory alignment reasons, will be effectively increased to 4 bytes on a 32 bit system and 8 bytes on a 64 bit system.
Consequently, an empty class instance will occupy a total of 12 bytes on a 32 bit system or 24 bytes on a 64 bit system.
Jignesh TrivediRank #16 · 50.3K reputationPosted Sep 18, 2013, 8:06 PM EDT
hi,
I think, If you create object on dos/ windows 64 bit environment, the size of empty class will be 1 becoz the sizeof() function return atleast 1 byte. it is never zero. in other os it might be 2 or 4 byte.
In C++ context
please refer
http://www.geeksforgeeks.org/why-is-the-size-of-an-empty-class-not-zero-in-c/
hope this will help you.