On the CLR memory principle of C #

Source: Internet
Author: User

On the CLR memory principle of C #

Submission: shichen2014 font: [Increase decrease] Type: Reprint time: 2014-08-04 I want to comment

This article mainly introduces the CLR memory principle of C #, to help readers understand the operation principle of C #, the need for friends can refer to the following

This article briefly describes the CLR memory principles of C #. There is no register in the memory that we are concerned about, so we are concerned only with the managed heap (heap), stack, string Chang (where string is a very special object)

First we look at two methods:

?
12345678910111213 void M1(){ string name = "Tom"; M2(name);} void M2(string name2){ int length = 10; double rate = 10.0; name2 = "Joe"; return;}

Here we have a M1 method, initialize a name local variable, and then call M2 ......

As shown in the first look at the operating principle of M1:


First, in one of the most basic methods, you should include some "prologue" code (not our tube, compile auto-generated), be responsible for initialization before the method starts to do its work, and there should be "epilogue" code that is responsible for cleaning up when the method is completed before returning to the caller. M1 start is that its "prologue" code allocates local variable name memory on the thread stack, and it is negative tom,tom this string in the string constant pool. M1 calls M2, passing the local variable name as an argument. This causes the address in the name local variable to be pressed into the stack. Inside the M2 method, the stack location is identified using a variable named name2. In addition, when a method is called, a "return address" is also pressed into the stack. The called method should return to this location (the location of the call) after the end.

Then take a look at the operating principle of the M2 shown:


M2 's "Prologue" code allocates memory for length and rate, and then M2 code to start executing the code. Eventually, M2 arrives at its return statement, causing the CPU's instruction pointer to be set to the return address in the stack (say {return address} in our diagram);

Note: Here specifically to mention that name2,name2 at the time of the call has become a parameter pressed into the stack, so here just to re-point to the address here, string memory play can see the string constant pool of related introduction

On the CLR memory principle of C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.