Analysis on Memory Management in C # Programming

Source: Internet
Author: User
C # One advantage of programming is Program Users do not need to care about specific memory management, especially the Garbage Collector will process all the memory cleanup work. Although you do not need to manually manage the memory, if you want to write high-quality Code To understand what happened in the background and the memory management of C. This article mainly introduces the situation in computer memory when allocating memory to variables.

C # divide data into two types: Value Data Type and reference data type. These two types of data are stored in different places in the memory: value data type is stored in the stack, the reference type is stored in the memory hosting heap.

1. Memory Overview
Windows uses a virtual addressing system. This system maps available memory addresses of programs to actual addresses in the hardware memory. The actual result is that every process on the 32-bit host can use 4 GB of memory. Of course, the 64-bit host is too large. The 4 GB memory actually contains all the parts of the program: executable code, DLL, and the content of all the variables used when the program is running. The 4 GB memory is used as the virtual address space or virtual memory. For convenience, memory is used here.

Each storage unit in 4 GB is stored from scratch. To access a value in a space in the memory, you must provide a number that represents the storage unit. In advancedProgramming LanguageA major role of the compiler is to change variable names that people can understand into memory addresses that the processor can understand.

2. Stack
In the memory, there is a region that becomes a stack and stores objects.

When calling a method, the value data type of the object member is a copy of the parameters passed to all methods. Note: When calling a method, the stack stores copies of all parameters. Therefore, the value of Value Type A is passed to the function, and the value of a does not change. Of course, the reference type will change, because the address of the reference type is stored in the stack, which will be detailed later.

The following code illustrates how a stack works:

 

{
Int;
// Do something;
{
Int B;
// Do something
}

}

First declare a, declare B in the internal code block, and then terminate the internal code block. Then, B is out of scope and A is out of scope. Therefore, the lifecycle of B is always included in the lifecycle of a. When a variable is released, the order of B is always the opposite of the order of memory allocation. That is, the lifecycle of a variable is nested. This is how the stack works.

3. managed heap
The stack has high performance, but the lifecycle of variables must be nested. This requirement is sometimes too harsh. We hope there is another way to allocate memory, store some data, and the data remains available for a long time after the method exits. In this case, the managed heap is used.

The managed heap (heap for short) is another area in the memory. We still use an example to illustrate how the heap works, as shown in the following code:

 

{
Customer customer1;
Customer1 = new customer ();
Customer customer2 = new customer ();
// Do something
}

First, declare a customer: mermer1 and assign a storage control to the reference on the stack. Note: Only allocating storage space for this reference is not an actual customer object. Customer1 occupies 4 bytes of space (32-bit machine) to indicate the address of the customer object in the memory.

Then, execute the second line of code to complete the following operations:

Allocate storage space on the stack to store the customer object. Note: Here is the customer object.

Set the value of customer1 to the memory address allocated to the customer object. From this example, we can see that the process of creating a variable of the reference type is more complex than that of a variable of the reward value type, and the performance is inevitably reduced. However, we can assign the value of a referenced variable to another referenced variable. When a variable is out of scope, it will be deleted from the stack, but the data of the object will still be stored in the memory, until the program stops.

In this way, when we pass a reference variable A to the function, we only pass the reference of variable A to the function, that is, only allocate memory on the stack, that is, variable B points to the same memory address. Therefore, variable A also changes when variable B changes.

4. packing and unpacking
Packing and unpacking are the conversion of the value type and reference type items. Packing can convert the value type to the reference type, which has the opposite effect. The reference type is converted to the value type.

5. Garbage Collection
Generally. . Net Runtime Library will run the Garbage Collector to release managed resources when it deems it necessary,

This is sufficient in most cases. That is to say, we do not need to care about memory. However, in some cases, we will force the garbage collection to run somewhere in the code to release the memory. This uses system. gc. Collect (). System. GC indicates a garbage collector. This is rare. For example, if a large number of objects in the code just stop referencing, it is suitable to call the garbage collector.

Summary

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 is retained in the heap until the program ends or the data is not applied by any variable.

From: http://hi.baidu.com/wch20088082008/blog/item/3d5eafc24a840c5db219a843.html

Related Article

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.