C # basic (I) Memory Management

Source: Internet
Author: User

Officially learning C #, Asp. net has been busy with a project for more than half a year, and has little time to look at the basic knowledge. I saw a post two days ago and suddenly found my basic knowledge so weak, there are many problems: "knowing why doesn't you know why?" The lack of basic knowledge cannot reach the level you want. So now I want to take the time to look at the basics. However, I don't want to take a look at the thick "C # advanced programming" and "ASP. net2.0 advanced programming, so I want to know where to look, not in order, but for results.

However, after reading the book, I want to write something. One is to deepen understanding and impression, and the other is to discuss with friends. I have never written a seriesArticleAnd start from here. Although it's all about writing basic things, it's easy for the experts, but it's the most important thing for beginners like me.

So, write this series:

1. Memory Management

 

 

C # One advantage of programming isProgramUsers 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-qualityCodeTo 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 # There are two types of data: Value Data Type and reference data type. These two types of data are stored in different places in the memory:The value data type is stored in the stack, and the reference type is stored in the memory managed heap..

I. Memory Introduction

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.

Ii. Stack

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

    1. Value data type of object members
    2. Copies of parameters passed to all methods when calling a method

Note: When a method is called, The stack stores copies of all parameters. Therefore, the value of a will not change when it is passed to the function by value type. 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:

 
1:{
 
2:IntA;
3:// Do something;
 
4:{
 
5:IntB;
 
6:// Do something
 
7:}
 
8:}

Declare a first, and declare B in the internal code block. Then the internal code block is terminated, 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:Variable lifecycles are nested.. This is how the stack works.

Iii. 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:

 
1:{
 
2:Customer customer1;
 
3:Customer1 =NewCustomer ();
 
4:Customer customer2 =NewCustomer ();
 
5:// Do something
 
6:}

First, declare a customer: mermer1. Allocate storage controls to this 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:

    1. Allocate storage space on the stack to store the customer object. Note: Here is the customer object.
    2. 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, know that the program is stopped.

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. Both point to the same memory address. Therefore, variable A also changes when variable B changes.

Iv. 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

In general, the. NET Runtime Library runs the Garbage Collector to release managed resources when it deems it necessary, which 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.

Vi. Summary

C # divide the memory into two areas: Stack and heap. Stack storage value type, stack storage reference type .. . Net Runtime Library will run the Garbage Collector to release the memory when it deems it necessary.

 

 

 
 
 
 
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.