Getting started understanding of C # heap and stacks

Source: Internet
Author: User

1. Stack area (stack)

The compiler automatically allocates releases, stores the function's parameter values, the values of local variables, and so on.
2, heap area (heap)

Usually released by the programmer, if the programmer does not release, the program ends may be recycled by the OS.

3. Stored data types

Heap: stores reference types. For example: classes, interfaces, arrays, and so on.

Stack: stores a value type that stores fixed-length data. For example: integers, characters, structures, Booleans, enumerations, and so on. Each program has its own stack at execution time, and other programs cannot access the stack.

explain one ,

Stacks are allocated memory space during compilation, so your code must have a clear definition of the size of the stack;

A heap is a dynamically allocated memory space during a program's run, and you can determine the size of the heap memory to allocate depending on how the program is running

explanation two ,

Store in the stack when the order of storage, maintain the principle of advanced, he is a continuous memory domain, there is automatic system allocation and maintenance.

The heap is unordered, he is a discontinuous memory domain, has the user to control and release, if the user does not release themselves, when the memory reaches a certain value, through the garbage collector (GC) to recycle.

Reference types are always stored in the heap.

Value types and pointers are always placed where they are declared.

Call method: The system first puts a piece of code (the header address of the heap) on the stack, followed by the parameters of the placement method. Then the code executes to the method, finds all the parameters of the heap header address in the stack, and controls the heap through the heap's header address.

Reference type: Always put in the heap.

When we use reference types, we are actually only working with pointers of that type. Instead of the reference type itself, the value type is used by itself.

Third, stack vs heap: Difference?

Stacks usually hold the steps that our code executes

And the heap is mostly objects, data and so on.

We can think of the stack as a box that is stacked together (the higher the memory address is the lower). When we use it, each time we take a box from the top, when a method (or type) is called, it takes away from the top of the stack (called a frame, the call frame), and then the next.

Stack memory does not need to be managed by us or by GC. When the top element of the stack is finished, it is released immediately. The heap requires a GC (garbage collection: garbage collector) cleanup.

Heap is not, like a warehouse, the storage of the various objects we use information, unlike the stack is that they are not immediately cleared out.

The difference between heap and stack can be seen in the following analogy:
Use the stack like we go to a restaurant to eat, just order (send application), pay, and eat (use), eat enough to go, do not bother to cut vegetables, wash vegetables and other preparation work and washing dishes, brush pots and other finishing work, his advantage is fast, but the freedom is small.
The use of the heap is like a DIY dish that you like to eat, more trouble, but more in line with their own tastes, and great freedom.

Iv. How are reference and value types assigned?
Let's take a look at two ideas:
Point 1, the reference type is generally allocated on the heap. A value type variable that is typically allocated on the stack
Point 2, value types and pointers are always assigned to the defined place, and they are not necessarily assigned to the stack. The reason is that if a value type is declared outside a method body and in a reference type, it is allocated on the heap.

The stack, mentioned above, maintains a dedicated thread stack for each thread while the program is running.
When a method is called, the main thread starts in the metadata of the owning assembly, finds the called method, and then compiles the result (typically the local CPU instruction) on top of the stack by JIT-on-demand. The CPU takes instructions from the top of the stack via the bus, and the driver executes.

This essay has to organize other master's notes, there is inappropriate and better explanation or supplement please leave a message, thank you!

Getting started understanding of C # heap and stacks

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.