C # stack description

Source: Internet
Author: User
First, the stack and heap (managed heap) are both in the virtual memory of the process. (The virtual memory of each process on a 32-bit processor is 4 GB)
Stack
Storage value type in the stack.
The stack is actually filled down, that is, the high memory address points to the local memory address.
The stack works by allocating memory variables and releasing them (Advanced and then outgoing ).
The variables in the stack are released from the bottom up, which ensures that the rules that come first in the stack do not conflict with the life cycle of the variables!
The stack performance is very high, but it is not flexible for all variables, and the lifecycle of variables must be nested.
We usually want to use a method to allocate memory to store data, and the data can still be used for a long time after the method exits. In this case, you need to use the heap (managed heap )!
Heap
Stack (managed stack) Storage reference type.
This heap is not another heap, And the heap in. NET is automatically managed by the garbage collector.
Unlike the stack, the stack is allocated from the bottom up, so free space is on the top of the used space.
For example, create an object:
Customer Cus;
Cus = new customer ();
Declare the reference of a customer, and allocate storage space to the reference on the stack. This is just a reference, not an actual customer object!
Ocus occupies 4 bytes of space and contains the reference address for storing the customer.
Next, allocate the memory on the stack to store the instance of the customer object. Assume that the instance of the customer object is 32 bytes, in order to find a storage location for the customer object on the stack.
The. NET Runtime library searches the heap for the first instance that has never been used and is a 32-byte continuous block storage customer object!
Then, assign the address assigned to the customer object instance to the cuz variable!
From this example, we can see that the process of creating an object reference is more complex than that of creating a value variable, and the performance cannot be reduced!
In fact, the. NET Runtime Library saves the correct status information. When adding new data to the heap, the reference variables in the stack also need to be updated. Performance loss!
There is a mechanism to allocate variable memory without being limited by the stack: Assign the value of a referenced variable to a variable of the same type, the two variables reference objects in the same heap.
When an application variable is out of scope, it is deleted from the stack. However, the data of the referenced object remains in the heap. When the program ends or the data is not applied by any variable, the garbage collector will delete it.

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.