C # Difference Between Stack and stack (to be updated and summarized 2)

Source: Internet
Author: User

C # differences between heap and stack (to be updated and summarized)
Thread Stack: Stack stack for short
Managed heap

When developing programs using the. NET Framework, we don't need to worry about the memory allocation problem, because the GC manager gives us everything. If we write the following two sections of code:
Code Segment 1:

Public int addfive (INT pvalue)
{
Int result;
Result = pvalue + 5;
Return result;
}

Code Segment 2:

Public class Myint
{
Public int myvalue;
}

Public Myint addfive (INT pvalue)
{
Myint result = new Myint ();
Result. myvalue = pvalue + 5;
Return result;
}

Question 1: Do you know how pvalue and result are stored in the memory and the lifecycle when code segment 1 is executed? What about code segment 2?
To solve the above problems, we should have a clear understanding of the stack and the managed heap (HEAP) under. net. If you want to improve program performance, understand the stack and heap, you must!
In this article, we will start from the stack, stack, and type variables to explain the program we have written.
C # When the program runs on the CLR, the memory is logically divided into two major blocks: Stack and heap. These two basic elements form the running environment of our C # program.

I. Stack vs Stack: What is the difference?

The stack usually stores the steps for executing our code, such as the addfive () method, int pvalue variable, int result variable, and so on in code segment 1. Objects and data are mostly stored on the stack. (Translator's note: Ignore Compiler Optimization) we can think of the stack as a box stacked together. When we use it, each time we take a box from the top. The same is true for the stack. When a method (or type) is called, it is removed from the top of the stack (called a frame, annotation: Call frame), followed by the next one. The heap is not a repository. It stores the various objects we use and other information. Different from the stack, they are not cleared immediately after being called.

1. Stack and stack

 

(Figure 1)

Stack memory does not need to be managed or managed by GC. When the top element of the stack is used up, it is immediately released. GC (garbage collection: Garbage Collector) is required for the heap.

2. What elements are allocated to the stack? What is allocated to the heap?

During program execution, there are four main types in the stack and heap: Value Type, reference type, pointer, and instruction.

Value Type:
In C #, the type inherited from system. valuetype is called the value type, mainly including the following (clr2.0 supports adding types ):
* Bool
* Byte
* Char
* Decimal
* Double
* Enum
* Float
* Int
* Long
* Sbyte
* Short
* Struct
* Uint
* Ulong
* Ushort

Reference Type:
The following is a reference type inherited from system. Object:
* Class
* Interface
* Delegate
* Object
* String

Pointer:
In the memory area, a reference to a type is usually called a "Pointer". It is managed by CLR (Common Language Runtime: runtime in public language) and cannot be displayed and used. It should be noted that a type of reference, that is, pointer and reference type, are two completely different concepts. A pointer occupies a memory area in the memory. It only represents one memory address (or null), and the other memory area it points to is our real data or type. 2:

(Figure 2)

Command:
Next, we will introduce the commands.

3. How to allocate?
Let's take a look at two points:
Viewpoint 1: The reference type is always allocated to the stack. (Correct ?)
Viewpoint 2: value types and pointers are always allocated to the defined place, and they are not necessarily allocated to the stack. (This is a bit difficult to understand and needs to be done slowly)

The stack mentioned above maintains an exclusive thread stack for each thread when the program is running.
When a method is called, the main thread starts to search for the called method in the metadata of the Assembly, and then uses JIT to instantly compile the result (generally a local CPU command) place it on the top of the stack. The CPU extracts commands from the top of the stack through the bus, and the driver executes them.

The following is an example.

Or the code segment 1 listed in the beginning:

Public int addfive (INT pvalue)
{
Int result;
Result = pvalue + 5;
Return result;
}

When the addfive method starts to be executed, the method parameters are allocated on the stack. 3:

(Figure 3)

Note: The method does not survive in the stack. The illustration is for reference only.
Next, the command points to the inside of the addfive method. If this method is executed for the first time, JIT real-time compilation is required first. 4:

(Figure 4)

When the method starts to execute internally, the variable result is allocated to the stack, 5:

(Figure 5)

After the method is executed and the method is returned, 6 is shown:

(Figure 6)

After the method execution is complete, the area on the stack is cleared. 7:

(Figure 7)

As shown above, a value type variable is usually allocated on the stack. So what is the understanding described in viewpoint 2? "Value types and pointers are always allocated to the defined place, and they are not necessarily allocated to the stack ".
The reason is that if a value type is declared in a method in vitro and in a reference type, it will be allocated on the stack.
Or code Segment 2:

Public class Myint
{
Public int myvalue;
}

Public Myint addfive (INT pvalue)
{
Myint result = new Myint ();
Result. myvalue = pvalue + 5;
Return result;
}

When the thread starts to execute the addfive method, the parameter is allocated to the stack, as shown in Figure 8:

(Figure 8)
Because Myint is a reference type, it is allocated to the stack and generates a pointer (result), 9:

(Figure 9)
Case 10 when the addfive method is executed:

(Figure 10)

The memory on the stack is cleared, and the stack still exists. 11:

 
(Figure 11)

When the program requires more heap space, GC needs to clean up the garbage, pause all threads, and find all non-reachable objects, that is, no referenced objects, to clean up. And notify the pointer in the stack to point to the object after address sorting again. Now we should know the importance of stack and heap for developing high-performance programs. When we use the reference type, it is generally the operation on the pointer rather than the reference type object itself. However, the value type operates on itself.
Next, we will illustrate this with examples.

Example 1:

Public int returnvalue ()
{
Int x = new int ();
X = 3;
Int y = new int ();
Y = X;
Y = 4;
Return X;
}

The execution result is 3, which is slightly modified:

Example 2:

Public class Myint
{
Public int myvalue;
}

Public int returnvalue2 ()
{
Myint x = new Myint ();
X. myvalue = 3;
Myint y = new Myint ();
Y = X;
Y. myvalue = 4;
Return X. myvalue;
}

The execution result is 4.

Let's analyze the cause. instance 1 has the same effect as the following code:

Public int returnvalue ()
{
Int x = 3;
Int y = X;
Y = 4;
Return X;
}


As shown in figure 12, both X and Y occupy a memory zone on the stack and do not interfere with each other.

(Figure 12)

For example 2, the utility is the same as that of the following code:

Public int returnvalue2 ()
{
Myint X;
X. myvalue = 3;
Myint y;
Y = X;
Y. myvalue = 4;
Return X. myvalue;
}
13,

 
(Figure 13)
The pointer X and Y on the stack point to the same region on the stack. Modifying the pointer will change the data on the stack.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/Zevin/archive/2010/07/13/5731965.aspx

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.