. Net types, objects, line stacks, and the relationship of the managed heap runtime

Source: Internet
Author: User
Tags class manager instance method

JIT (just in time) compiler

Next, we'll talk about the invocation of the method, where we'll start with the JIT compiler. Take the code in the CLR book for example (hand-hitting ...). Take the main method as an example:

static void Main(){   Console.WriteLine("Hello");   Console.WriteLine("XiaoCong");}
    1. First the CLR detects that a console type is referenced in the main method, and the CLR assigns it an internal structure.
    2. Each method in the internal structure corresponds to a record entry that holds an address in the record entry, and the implementation of the method can be found based on this address.
    3. When the structure is initialized, the record entry is pointed to the Jitcompiler function.

    4. When Wirteline is executed for the second time, the JIT function is skipped and the code in the memory block is executed directly because the first validation and compilation has occurred.

Blogging efficiency is low (ORZ) ...

Types, objects, line stacks, managed heap run-time interrelationships

The following is a note of the object and type objects to distinguish

The first time the process runs, a System.Type type object is created on the managed heap (explained behind the article). Then the thread is created with a stack of 1MB size.

First a piece of code (modified on the basis of the code in the book)

InternalClass employee{  PublicInt32 getyearsemployed () {...}Non-virtual instance method (instance method) PublicVirtual String Genprogressreprot () {...}Case method (virtual method) PublicStatic Employee LookUp (String name) {...}//static method} internal sealed class manager:employee{ public override String genprogressreport () {...} //override Method (virtual method)} void M1 () { string Name= ...  return}void M2 (String str) {  Employee E;  Int32 year;  E = new Manager ();  E = Employee.lookup (" Zhangsan ");  year = e.getyearsemployed ();  E.genprogressreport ();           

1. First execute the M1 method, the 1MB stack space will be created on the thread stack, then the prologue code and the ending code will be added

Prologue code: Perform some initialization variable operation (initialize null or 0)

End code: Press in "Return address"

2. Then execute the M2 method, pressing the parameter and return address, and local variables into the stack

3.CLR to ensure that the assembly is fully loaded, and then create the data structure (type Object) of the M2 Internal Reference object

    1. The data structure contains a type object pointer, a synchronous block index, a static data field (including a field in the base class, the number of bytes allocated by the object itself), and a method table.
    2. (String and Int32 are commonly used and may have been created during the actual encoding process, not shown in the diagram.) )
    3. The type Object pointer of the manager and the employee is pointing to type. Type points to itself.
      Type objects are also objects, and the CLR must initialize these programmers when they are created. When the CLR runs in a process, it immediately creates a special type object of type System.Type.
      Both employee and manager are "instances" of that type. So their object pointer will point to type.
4. The M2 method then executes the construction manager object C# e = new Manager();

This creates an instance of the manager type object in the managed heap. That is, the manager object (Note the difference between the distinguished and type objects).

The object also contains the type object pointer and the synchronization block index. It also contains the necessary bytes to accommodate all instance data fields (including base class fields) defined by the manager type.

the CLR automatically initializes the content-type object pointer so that it references the Manger type object. The synchronization block index is also initialized and all instance fields of the object are set to null or 0, and then the type constructor is called (modifies instance field data).

The new operator returns the memory address of the manager object and stores the address in the variable E.

5.M2 next call to the static lookup method C# e = Employee.LookUp("ZhangSan");

When a static method is called, the CLR navigates to the type object of the static method's type object (Employee type Object). It then finds the record entry in the corresponding method table, JIT-compiles the method (the first time it executes the method), and then calls the JIT-generated CPU instruction. Assuming that the method finds Zhangsan in the database and then returns a completely new manager object, the lookup method constructs a completely new manager object on the heap and initializes it with the Zhangsan database information. The address of the returned object is then saved in the variable E. The old manager object then waits for the garbage collector to reclaim the release.

6.M2 Next Call to instance method C# year = e.GetYearsEmployed();

Invoking an instance method, the JIT compiler finds the type object (Employee type Object) that emits the type of the calling variable (here is the type of employee e). The JIT then looks for the record entry, compiles the method, and executes the CPU instruction.

If the employee type does not define that method, it is searched along the base class until the type of object. It is possible to find along the way because each type of object has a field that references its base type.

Assuming that the method returns 5, year will be 5.

7.M2 Next Call to virtual method (overridden by manager) C# e.GenProgressReport();

Call the virtual method, JIT to generate some extra code in the method. The code first checks the variables that emit the call, and then follows the address to find the object that made the call (this is the new manager object). The code object then internally "type Object pointer", then finds the record entry in the Type Object (Manager type Object) method table, compiles the Chengcheng CPU code.

If the lookup method finds an employee type, the Genprogressreport method for the employee type is performed here.

8. After executing the M2 method, the return address is found, returning the M1 method to continue execution after 9.m1 execution completes, returning the method that calls M1 to continue execution

Article Source: http://xcong.cnblogs.com

. Net types, objects, line stacks, and the relationship of the managed heap runtime

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.