C # Programming (73)----------Analysis of memory management in C #

Source: Internet
Author: User

Analysis of memory management in C #

The individual feels that C # absorbs the advantages of various languages, which is a synthesis, but it is not known, but this set of all languages in one situation is good or bad. One of the advantages of C # programming is that programmers do not need to be concerned with specific memory management, especially when the garbage collector handles all the memory cleanup work. Although it is no more than manually managing memory, if you want to write high-quality code, understand what happens in the background and understand C # 's memory management.

Users can get as efficient as the C + + language without having to consider the complexities of memory management like in C + +.

Note: Many of the contents of this chapter, can say all, are not verified by facts. You should consider this session as a simplified guide to general rules, rather than an exact description of the implementation.

First C # divides data into two types: the value data type and the reference data type, which are stored in different places in memory: The value data type is stored in the stack, and the reference type is stored in the managed heap of memory.

I. Introduction to Memory

Windows uses a system: a virtual addressing system. The purpose of this system is to map the memory addresses available to the program to the actual addresses in hardware memory. The actual result is that each process on a 32-bit machine can use 4GB of memory, of course, 64-bit machines 4, This number is even bigger. This 4G of memory actually contains all the parts of the program, executable code, DLLs, and the contents of all the variables used by the program when it runs. This 4GB memory is called the virtual address space or virtual memory. For convenience, the memory becomes.

Each storage cell in 4GB is stored up from zero. To access the value stored in a space in memory, you must provide a number that represents that storage unit. In high-level programming languages, one of the most important roles in a particular issue is the memory address that is responsible for programming the processor with variable names that people can understand.

Two. Stacks

In memory, there is an area called a stack, which stores objects.

A copy of the parameter that is passed to all methods when the value data type of the object member is called. Note: When a method is called, the stack stores a copy of all parameters, so the value of type A is not changed when passed to the function. Of course, the reference type will change, because the address of the reference type is stored in the stack.

Case:

{

int A;

Do domething

{

int b;

Do domething

}

}

Analysis: First declare a, declare B in the internal code block, then the internal code block terminates, B is out of scope, and finally a out scope. So the declaration period of B is always contained in the declaration period of a, and when the variable is released, its order is always the opposite of the order of memory allocated. That is: The lifetime of a variable is nested. That's how the stack works.

Three. Managed heap

Stacks have fairly high performance, but the life cycle of variables must be nested, and this requirement is sometimes too demanding. We want to have a different way of allocating memory, storing some data, and, for a long time after the method exits, the number of households is still available, and the managed heap can be used.

The managed heap (heap) is another area in memory, and we still use an example to illustrate how the heap works:

{

Customer Customer1;

Customer1=new Customer ();

Customer Customer2=new customer ();

Do domething

}

Analysis: First, declare a customer:customer1 and allocate space on the stack for this reference. Note: Simply allocating storage space to this reference is not the actual customer object. Customer1 occupies 4 bytes of space (32-bit machine), To represent the address of the customer object in memory.

And then. Execute the second line of code to complete the following actions:

Allocate storage space on the heap to store the customer object, note that this is the customer object.

Set the value of the variable customer1 to the memory address assigned to the customer object from this example, it can be seen that the process of building a variable of a reference type is more complex than establishing a value type variable, and does not prevent performance degradation. However, we can assign the value of a reference variable to another reference variable, When a variable is scoped, he is removed from the PVP, but the object's data remains in memory until the program stops.

In this way, when we pass a reference variable A to the function, we simply pass the reference to the variable A to the function, which is simply allocating memory on the station, that is, the variable B points to the same memory address. Therefore, when a variable changes, variable a also changes.

Four. Unboxing

Unpacking is the conversion of a value type and a reference type, and boxing can convert a value type to a reference type, and the unboxing acts exactly the opposite, and the reference type is converted to a value type.

Five. Garbage collection

In general the case. . The NET runtime will run the garbage collector to release the managed resources when it deems it necessary, which in most cases is sufficient, that is, we do not need to care about memory. But in some cases, we force the garbage collector to run somewhere in the code, Frees up memory. This uses the System.GC.Collect; SYSTEM.GV represents a garbage collection period. This is rare, for example, where a large number of objects in the code just stop referencing, that is, and call the garbage collector.

C # Programming (73)----------Analysis of memory management in C #

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.