The relationship between C # runtime

Brief introduction

Record c # objects in the relationship between the managed heap at runtime, as recorded in the life cycle of a method of execution time, when the prior method, CLR will first perform the method used inside all local variables, parameters corresponding memory address, etc. All holds the current thread stack them, and all instances of the field will be set to null or 0, the object will not return after the new operating memory address.

 

Thread stacks

 When 1.CRL run will start a process that includes multiple threads, created during the time the thread assigned to 1MB stack, stack space used to pass arguments to the method, the method local variables are defined inside on the stack.

 

 

 2. The easiest way to contain initialization code to initialize method before beginning work, also contains the end code, to clean up after their work method, the following figure shows the time of M1 method to begin its initialization code in thread stack the local variable name memory allocation, and memory will be used to store a name in the name of the stack open

 

3. Method M2 then executes, the local variable name as arguments, there is actually a delivery address that is referenced to a memory location where the variable name, and will also save the return address onto the stack, is called after the end of the method, it should return to the position, as shown below:

 

 

4. When performing the method M2 starts, initialization code to his local variable len count and allocate memory in the thread stack, after the completion of internal code execution method M2, M2 begin Return statement, the CPU causes the instruction pointer is set to the stack return address, M1 will eventually return to its caller:

 

Class storage

 If there is now a defined class, name now based on this class to be discussed:

public class User{
 int age=18;
 string name="zyz";
 public  string getUserName(){...}

}

当window已经启动,CLR以加载到其中,并且托管堆也已初始化,c#编译器会把C#代码编译成IL(中间语言)并且由CLR当中的JIT把IL代码转换成本机cpu指令, JIT在编译的时候会生成。

在执行的时候M3内部引用的所有类型会注意到包括user,int,string类型,这时CLR要确定定义了这些类型的所有程序及都已经加载,然后利用程序集提取与这些类型相关的信息,创建一些数据结构来表示类型本身。如下图所示:

 

 

 

 

当CLR确认方法当中所有类型都已经创建,M3代码编译之后就允许线程执行M3的代码,M3的构造函数执行时必须在线程栈中为局部变量分配内存,  

 

 

当实例化User的时候,将会在托管堆中创建user类型的一个实例,并且该实例也有类型对象指针和同步块索引,该对象还包含了必要的字节来容纳user类型定义的所有数据字段(age,name),以及容纳由user的任何积累(本例只有Object,所有对象都继承system.object)定义的所有字段,任何时候在堆上新建对象,CLR都自动初始化内部的类型对象指针成员来引用和对象对应的类型对象。

 

 

当调用M3方法当中进行调用getUserName的时候,会对方法进行JIT编译,在调用JIT编译好的代码。

 

 

 

如有理解有误的地方,还请给与指正!~

Guess you like

Origin www.cnblogs.com/zhengyazhao/p/11334279.html