. NET heap and Stack 01, basic concept, value type memory allocation

Source: Internet
Author: User

When we understand some of the fundamentals of the. NET framework, it is really necessary to understand some of the underlying knowledge. For example, how the. NET Framework is memory-managed, how it is garbage collected ... In this way, we can write more high-performance programs.

In the. NET Framework, there are 2 places for us to save management data: One is "heap", also called "Managed Heap", managed by the. NET Framework's garbage collector (garbage Collection, GC), and the other is "stack", also called "Thread stack". Managed directly by the operating system. They are all hosted in operating system memory.

This article mainly includes:
What is "stack"
What is "heap"
Value type memory allocation
※ Value type memory allocation within a method
※ Value type memory allocation inside the reference type
Exercises

What is "stack"

You can think of "stacks" as boxes stacked on top of each other, where instances of value types are stored.

In an application, whenever a method is called, it is equivalent to placing a box A on the heap, at which point the application can only use the box A, which is at the top of the stack, just put on, and when the method executes, it is equivalent to throwing the top box a away. Next, the box B just below A is at the top of the stack, and the application starts using box B, and so on. Also, whenever the top box is thrown away, its corresponding memory is automatically released.

The advantages of the stack are high execution efficiency and the disadvantage of limited storage capacity.

In the. NET framework, all that derive from System.ValueType is a value type, and the value type instance is on the stack. Value types include:
bool
Byte
Char
Decimal
Double
Enum
Float
int
Long
SByte
Short
Stuct
UInt
ULong
Short

What is "heap"


You can think of "heap" as some sort of unordered box, where a reference type instance is stored.

We can use any box at any time. We need to use the automatic recycling mechanism of the garbage collector (garbage Collection, GC) or manual processing to ensure that the "heap" box is recycled in a timely manner.


In addition, depending on the size of the reference type instance, the heap is divided into the GC heap and the LOH (Large Object heap) heap, and when the reference type instance size is less than 85,000 bytes, the instance is assigned to the GC heap, and when the instance size is greater than or equal to 85,000 bytes, The instance is assigned to the "LOH (Large Object heap) heap".

In the. NET framework, all that derive from System.Object is a reference type, and the reference type instance is in the heap. Reference types include:
Classes Class
Interface interface
Commissioned delegate
Object
String

Value type memory allocation

Value type memory allocation within a method

public int Add (int x) {int result;    result = x + 2; return result;}

1. Before executing the Add (int x) method, the method parameter x is stored at the top of the stack.

2, in the "Method table" search for the Add () method, if not found, let JIT compile in time and then stored in the "method table" to go.

3. Start execution of the Add (int x) method, local variable result also requires some memory in the "stack".

4. When the method executes, the result is released first, and then X is released, and the thread stack pointer is pointed again.

Value type memory allocation inside a reference type

public class myclass{public int myvalue;}    Public MyClass Add (int x) {MyClass result = new MyClass (); Result.    myvalue = x + 2; return result;}

1. Before executing the Add (int x) method, the method parameter x is stored at the top of the stack.

2, in the "Method table" search for the Add () method, if not found, let JIT compile in time and then stored in the "method table" to go.

3. Start execution of the Add (int x) method, execute MyClass result = new MyClass ()
First create an instance of MyClass on the managed heap, and then open a space on the stack and point to the instance address.

4, when the method is completed, in the stack from top to bottom in order to free memory.

At this point, how do the MyClass instances on the managed heap handle?

5. At this point, the garbage collector comes in, and he searches the managed heap for instances of objects that are no longer referenced, and then implements the recycle.

Exercises

 Public int returnvalue ()           {                 int x =  new int ();                 x = 3;                 int y = new int ();                 y = x;                       y = 4;                            return x;           } 

The result is: 3, because the value type x variable is present on the stack until the end of the method execution.

Public int returnvalue2 ()           {                 MyInt x =  New myint ();                 x.MyValue = 3;                 myint y = new myint ();                 y = x;                                   y.MyValue = 4;                                return x.myvalue;          } 


The result is: 4, because when you assign X to Y by y=x, you actually assign x to the address on the managed heap y, that is, the X and Y on the stack point to the same object instance on the managed heap, changing the field value of Y, which is equivalent to changing the field value of X.


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.