2

I am new to the programming in C#.

Can anyone please tell me memory management about C#?

Class Student
{

     int Id;
     String Name;
     Double Marks;

     public string getStudentName()
     {
         return this.Name;
     } 

     public double getPersantage()
     {
         return this.Marks * 100 / 500;
     } 
}

I want to know how much memory is allocated for instance of this class?

What about methods? Where they are allocated?

And if there are static methods, what about their storage?

Can anyone please briefly explain this to me?

1
  • A good tool for understanding the garbage collector is the CLR profiler. Commented Nov 21, 2009 at 20:06

4 Answers 4

5

An instance of the class itself will take up 24 bytes on a 32-bit CLR:

  • 8 bytes of object overhead (sync block and type pointer)
  • 4 bytes for the int
  • 4 bytes for the string reference
  • 8 bytes for the double

Note that the memory for the string itself is in addition to that - but many objects could share references to the same string, for example.

Methods don't incur the same sort of storage penalty is fields. Essentially they're associated with the type rather than an instance of the type, but there's the IL version and the JIT-compiled code to consider. However, usually you can ignore this in my experience. You'd have to have a large amount of code and very few instances for the memory taken up by the code to be significant compared with the data. The important thing is that you don't get a separate copy of each method for each instance.

EDIT: Note that you happened to pick a relatively easy case. In situations where you've got fields of logically smaller sizes (e.g. short or byte fields) the CLR chooses how to lay out the object in memory, such that values which require memory alignment (being on a word boundary) are laid out appropriately, but possibly backing other ones - so 4 byte fields could end up taking 4 bytes, or they could take 16 if the CLR decides to align each of them separately. I think that's implementation-specific, but it's possible that the CLI spec dictates the exact approach taken.

Sign up to request clarification or add additional context in comments.

7 Comments

Right! On a 64-bit architecture you get a 64 bit reference to the string.
Jon Skeet, I understand that on this board, your words themselves are usually worth citing as a source, so this isn't a challenge to that. Could you please document where you read, or how you discovered this information (namely the object oeverhead bullet). Thanks.
@munissor: Yes, and I believe the object overhead is bigger - but I don't know whether it's 12 bytes or 16.
@San Jacinto: Well, the size of int and double are specified by the language and framework. The size of the string reference is dictated by the CLR (so as munissor says it would be 8 bytes on a 64-bit CLR). The size of the object overhead is through experience/experimentation and probably reading CLR via C# as well :) Interestingly, if you use just "new object()" that still takes up 12 bytes on a 32-bit CLR: it's a minimum object size, effectively. So you get your first int for free :)
Thanks; I'm no .NET expert, but I had never seen documentation of how much memory the CLR uses for its own purposes when you create an object :)
|
0

As, I think Jon Skeet is saying, it depends on a lot of factors, and not easily measurable ahead of time. Factors such as whether it's running on a 64 bit OS or 32 bit OS must be taken into account, and whether you are running a debug or release version come into play. The amount of memory taken up by code depends on the processor that the JITTER compiles to, as different optimizations can be used for different processors.

Comments

0

Not really answer, just for fun.

struct Student
{
    int Id;
    [MarshalAs(UnmanagedType.LPStr)]
    String Name; 
    Double Marks; 
    public string getStudentName()
    {
        return this.Name;
    }      
    public double getPersantage()
    {
        return this.Marks * 100 / 500;
    }
}

And

      Console.WriteLine(Marshal.SizeOf(typeof(Student)));

On 64bit return:

      24

And on 32 bit:

      16

Comments

0
sizeof(getPersantage());

a good way to find out bytes for it. not too havent done much C#, but better with an answer than no answer :=)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.