Analysis of C # data types and Memory Management Mechanism (1)

Source: Internet
Author: User

Although C # (in fact all languages Based on. Net Framework) automatically handles the issue of memory allocation and release, and introduces the garbage collection mechanism, which provides comprehensive data type management capabilities. However, in many cases, it is very useful to understand its deep mechanism, which can greatly improve the program efficiency. Now, Phone7 is released on mobile devices and some special applications. It is very useful to intelligently and skillfully process memory management and various data types, so that you can better maintain and develop programs.

1. Windows Memory Management Mechanism

To learn more about the memory management mechanism of C #, you must first understand the memory management system of Windows. I remember a course called "operating system principles" in college. Don't hit me. This course is not good ...... Hh, the key is to mention the memory management system. Do you still remember the block ing and paging ing? The CPU maintains a table in the cache, and stores the ing between memory and data blocks on the disk. I will not talk about the specifics (I have made two chapters in the book to refer to this mechanism). It is to divide the memory space of a program into many blocks, there will be an algorithm that writes recently used memory blocks to the memory (RAM). The memory blocks can also be divided into high-speed caches so that the next access can be quickly read. Of course, there are also writing mechanisms involved, there are many ways, there are also conflict processing, lock or something, these mechanisms were so crazy that year, now think about the fact that the caching mechanism of websites and programs is not born out of these algorithms? It is much simpler. In a 32-bit system, the maximum memory space of a program is 4 GB, which is managed under the underlying operating system and divided into many segments, pages and blocks. What is in these memory blocks? Well, everyone knows-data and logic. The logic is the method in your code, indicating computing, running, and processing. The data is the object to be processed. In C ++, it is divided into simple data types and pointer data types. This data includes variables, constants, and object entities. Generally, data occupies the vast majority of the memory space of a program. It is also part of memory management.

2. C # Data Type

In fact, the Data Types in C # are the same as those in C ++, which are the value type and reference type respectively.

(1) The value type corresponds to the simple data type of C ++, such as int, double, bool, and the value type is.. Net Framework has 13 predefined types. For details, see the table below: type description.
Char 16-bit unicode Character
Sbyte 8-bit signed integer
Short 16-bit signed integer
Int 32-bit signed integer value default variable type
Long 64-bit signed integer Prefix: L
Byte 8-bit unsigned integer
Ushort 16-bit unsigned integer
Unit 32-bit unsigned integer Prefix: U
Ulong 64-bit unsigned integer Prefix: UL
Float 32-Bit Single-precision floating-point Prefix: F
Double 64-bit double-precision floating point default floating point variable type
Decimal 128-bit high-precision decimal causes performance loss. Prefix: M
Bool value: true/false


* Hexadecimal number plus prefix 0x, for example, 0x12ba;

Another point must be noted that the structure struct in C # is different from that in C ++, which is a value type and cannot be inherited. Struct is derived from System. ValueType.

The built-in type of. Net is also called the CTS type. The value type is stored in the stack area of the memory space (stack), and the stack in data structure (Advanced and later)

View sourceprint? 01 {

02 int p1;

03 int p2;

04 // code

05 {

06 int q1;

07 int q2;

08 // code

09}

10}
Stack top variable
700012-700015 p1
700008-700011 p2
700004-700007 q1
700000-700003 q2
Stack bottom

As you can see, the variable is the sequence in which the outer brackets are first stacked and the inner layer is followed by the stack. Release is the opposite. The stack pointer always points to the final variable into the stack and retains the pointer pointing to the next stack vacancy.

Such a structure will make variable reading very efficient, as long as the pointer is moved, the variable can be obtained and released. The disadvantage is that it is not flexible enough to manage a large data structure efficiently. For value types, the. net garbage collection mechanism is not required, and memory management can be performed well.

(2) The reference type corresponds to another C ++ data type, pointer type. This means that if we create a reference type, a memory space is actually allocated in the heap. Then this variable is actually a pointer to this memory block. in C #, there are two types of data as reference types: the first class is the reference type in the CTS type, there are two: object and string; the second class is various classes in C #, which must be inherited from objects (including custom classes and C # class libraries ). In a broad sense, the object type and the string type are also correct, because all C # (. Net Framwork) classes are inherited from the base class System. object.

Why do we need to separate the Object and String types? There is a reason for this. The string type is a special reference type.

View sourceprint? 01 using System;

02

03 namespace StringTest

04 {

05 class StringExpress

06 {

07 public static void Main ()

08 {

09 string s1 = "I am a string ";

10 string s2 = s1;

11 Console. WriteLine (s1 );

12 Console. WriteLine (s2 );

13 s2 = "I am a new string ";

14 Console. WriteLine (s1 );

15 Console. WriteLine (s2 );

16}

17}

18}

Since it is a reference type, what do you think should be displayed normally? Because the variable is just a reference (pointer), s1 and s2 should point to the same memory zone. In fact, when running

View sourceprint? 1 string s2 = s1;

S1 and s2 do point to the same string variable. The first two rows of output are:

I am a string

I am a string

Let's guess what the last two lines are? The result is:

I am a string

I am a new string

This is a special feature of the string type. When you assign a new value to the string type, a new object will be created without overwriting the original value. This is actually an operator overload, the referenced object is reassigned.

The Object class has many applications and features and will be discussed in future articles. Packing and unpacking, and some basic methods for classes are also defined in the Object.

3. Garbage Collection

Garbage collection is a very important mechanism in. Net. In fact, this mechanism ensures the high performance of. Net Framework. Garbage collection is an automatic mechanism for releasing referenced data. In C ++, this does not exist. All object entities must be explicitly released by code; otherwise, they will occupy the memory.

We know the reference type and maintain a pointer. This pointer is actually placed in the Stack, and the memory space to be pointed to is in the heap. GC (garbage collection) to release the heap memory is determined based on the reference pointer. If a memory block in the heap has no reference (the referenced variable is out of the range ), this memory will be released when garbage collection is in progress. In fact, the. Net garbage collection mechanism does not just do this. Usually, the data in the heap will become fragmented due to multiple releases and distributions. Become the memory block of the SWAp block, which causes trouble for allocating new memory space. Because when allocating new space, you need to traverse the entire heap space to determine a memory block that is sufficient to create an object instance, create reference variables, and maintain the Heap Storage records. This not only reduces the efficiency, but also reduces the memory usage.

GC performs two steps during garbage collection: 1. Release the memory space. 2. re-adjust and compress the heap area to move the remaining objects to one end of the heap. Although this will cause some additional resource overhead, after completing this step, instantiating an object and searching for access instances will be much faster, in fact improving the performance. This second step mechanism is also the main difference between managed heap and unmanaged heap.

The timing of garbage collection is controlled by the system, and the specific algorithms have not been published by Microsoft. We can also call the garbage collection mechanism as needed to explicitly execute System. GC. Collect (). But it is not recommended, because this process is actually resource-consuming and does not have high automatic collection efficiency. For the execution effect of the entire program, under normal circumstances, it is better to use the automatic collection mechanism of the system.

The next article will detail the garbage collection mechanism and the use of C # objects.

 

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.