简体   繁体   中英

C# basics - Memory Management

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?

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 (eg 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.

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.

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
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 :=)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM