Stack and managed Stack

Source: Internet
Author: User

This is the rain markArticleThe original Article is here.

Concerning the CLR memory management mode, various books and network articles are held in different words, which is very confusing. In most cases, we only mention "managed heap" and "stack" in general. The actual process is very complicated. Since there is no unified statement, I cannot guarantee the accuracy of this article.

When the CLR creates an execution thread, it allocates 1 MB of call stack memory to the current thread based on the ". stackreserve 0x00100000" command in mainfest. The call stack is different from the managed heap. It is not responsible for saving hosted reference objects, but for maintaining the order of method calls and executing il commands. When we call a method using call, callvirt, and other commands, CLR allocates a memory block called a stack frame in the call stack to execute the target method.Code. Is the memory of a stack frame.

Each stack frame contains three memories:Parameter table (argument table)Used to store method parameters,Local variable table)Storage Method local variables (. Locals init), andEvaluation Stack)This is the Il code execution area. Before executing the Il command, the data or address of the command is pushed into the computing stack. Then, the command is executed and the calculation result is displayed. When executing the next command, the operation is repeated, until the method call is completed. These three memories constitute the memory execution environment of a method. Note that the size unit is not a byte but a slot. Each slot can store a hosting unit (object). We can use the. maxstack command in the method body to control the size of the stack calculated by the current method.

. Method private hidebysig static void test1 (int32 & X) cel managed
{
. Maxstack 8
}

Return to the method call process. When we call a method using call and other commands, the CLR allocates a new stack frame for the target method on the call stack, then, press the method parameters from the calculation stack of the current method to the parameter table of the target method, and then execute the process to jump to the target method. When the. Locals init command is executed, the corresponding internal variables are created in its internal variable table, and the calculation stack of the target method is used to execute the Il command. When the target method il command ends, its return value (if any) is pop and pushed to the computing stack of the original caller. the RET command returns the control to the caller, the target method stack frame is discarded and the occupied memory is released.

Related Concepts

Stack and heap are different. Stack is a linear structure, while stack is a chain structure. The stack works according to the principle of LIFO-last in first out and maintains memory usage through a pointer. When we release a piece of memory, we only need to reply the pointer to a starting position, so its execution efficiency is very high. Managed heap is a dynamically allocated memory area managed by GC. All threads in each process share a managed heap.

. Net Clr and Java VM are both known as stack-base VMS. Their execution mechanism is different from the 80x86 method we are familiar, it does not have the concept of registers. Their Instruction Set uses stacks for operations. First, data is pushed to the stack and then operated. The stack here is the calculation stack we mentioned above ).

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.