C # interaction during runtime
This blog mainly describes the relations between runtime types, objects, thread stacks and managed stacks, the differences between static methods, instance methods and virtual methods, and memory allocation and recovery.
Thread Stack: A process may contain multiple threads. When a thread is created, it is allocated to a 1 MB stack, the stack is used to store the real parameters, parameters, and local variables in the method. The stack is built from the high memory address to the status address. Because the stack has advanced features, therefore, variables defined first are recycled.
The following is a simple example to better understand the thread stack.
Since the thread stack allocates memory from a high level, I will draw the allocation first. When I call the F1 () method, the memory allocation order is as follows: name-> N-> F2 return address-> Age-> name; the order of memory reclaim is of course the opposite. In a method, it should include some preludeCodeAnd some initialization work, as well as some final code, and some recycling work after the method is executed. Because the return address of the method is allocated first, return to the return address when the method is executed. Stack Overflow may occur when the recursion is too deep. Please refer to my 《Recursion shocked brother againBecause parameters and local variables can be recycled only when the method returns.
Let's take a look at two simple classes before introducing managed heaps:
Public Class Person
{
Private Int Height;
Public Void Setheight ( Int Height)
{
This . Height = height;
}
Public Virtual Void Say ( String Word ){}
Public Static String Head ()
{
Return " My head " ;
}
Public Static Int Age = 100 ;
}
Public Class Student: person
{
Public Override Void Say ( String Word)
{
Console. writeline (Word );
}
}
Static VoidMain (String[] ARGs)
{
Person student =NewStudent ();
Student. Say ("Hello CTH");
Student. setheight (172);
Person. Head ();
Console. Readline ();
}
CLR will load this object when it accesses an object for the first time. Here, when the student variable is defined, it will allocate memory to the person object in the thread stack. Will it load the object for the first time, before constructing a student object, load the student object, allocate memory to the student object, and construct a student object. The object address is stored in the local variable student in the thread stack. We know that the content of the type object includes: Type object pointer, Synchronous Index block, static fields and methods (static and non-static ), both type objects and instance types must have type object pointers and synchronized index blocks. We know that static fields belong to the class and are shared by all instances of this class, of course, the memory of the static field is allocated in the type itself, and the method is shared by all instances of the class. Its memory is also allocated in the type itself, each type object has a method table, and the methods defined in the class have a corresponding item.
When constructing an object instance, you only need to allocate memory for the Type object pointer, synchronization index block, and instance field of the object. For the object instance, type object pointers allow instances to access type objects such as Chinese and German static fields and methods.
Student is a local variable defined in the thread stack. It stores the address of an instance of student in the hosting heap. Therefore, student can access fields and methods in the student object, in fact, the access method is to access the object items in the method table of the type object student through the type object pointer.
The execution process of the "say" method: the variable student points to a student object. Of course, it calls the "say" method in the student object, even though it is of the person type when defining student, because it is a reference type, it points to the memory of the student object in the managed heap, and then traverses the method table of the object to find the method call.
Especially the virtual method, JITSome additional code is added to the virtual method. The code is executed every time the method is called. The code checks the variables that make the call and finds the objects to be applied based on the variable, and then call the method of this object. Without this code, you think the CLRWhether to call the methods of the parent class or call such methods, virtual methods bring convenience, while also adding the necessary check code.
The execution process of the setheight method is the same as that before the say method, but this method is not found when traversing the method table of the student object, we know that non-private methods defined in the parent class can be inherited by the quilt class because each type defines a field that references its base class, if the method called by a class is not defined by itself, the compiler will trace back the class hierarchy until the base class object finds the relevant method and calls it, if no relevant method is found, an exception is reported. Therefore, the setheight method actually calls the setheight method in person.
Head Method execution: because the head method is a static method different from the above two methods, when calling a static method, CLR will locate the type object of the static method object, find related record items in the method table of the corresponding instance object. If no record is found, the record will be traced back.
When student. setheight (172); when student is not referenced and becomes spam, it will be recycled before its method is returned, that is, the student instance object is recycled, release its memory, but the type object will not be recycled. The generation cycle of the type object is: the object is loaded into the CLR until its appdomain is detached. The static field is the identifier of the referenced type, so the object referenced by the static type will never be recycled. If it references a collection object and continuously adds elements to it, this will cause memory leakage. For more information about memory management garbage collection, please refer to another blog titled garbage collection-generation.
Author: Chen taihan
Blog: http://www.cnblogs.com/hlxs/