C#'s GC mechanism

Personal statement: All articles are written because of personal interviews

The GC mechanism is Garbage Collection. Garbage collection is different from the fact that C++ needs to destroy/release the memory every time it applies for memory. The GC mechanism of C# does not require us to manually release the requested memory.

This article mainly introduces

memory management pool

activation

trigger mechanism

GC operation

In order to save time, I posted the mind map

 

memory management mechanism

To talk about memory partitioning before recycling, the partitioning of c# is as follows

1) Stack area: automatically allocated and released by the compiler, storing the object itself of the value type, the reference address (pointer) of the reference type, the reference address (pointer) of the object in the static area, the reference address (pointer) of the object in the constant area, etc. It operates like a stack in a data structure.

2) Heap area (managed heap): used to store the reference type object itself. In c#, it is managed by the garbage collection mechanism (GC) of the .net platform. Both the stack and the heap belong to the dynamic storage area, which can realize dynamic allocation.

3) Static area and constant area: used to store static classes, static members (static variables, static methods), and constant objects themselves. Since the reference addresses in the stack are all pushed onto the stack at the beginning of the program running, the life cycle of the objects in the static area and the constant area will last until the end of the program running, and then the objects in the static area and the constant area will be released and recycled (automatically released by the compiler). Therefore, the use of static classes, static members (static variables, static methods), and constants should be limited, otherwise the program load will be high.

4) Code area: store the binary code in the function body.

Variables declared by the program will only be in stack or heap memory, depending on the type scope

Heap memory is generally used for the large space that you apply for (for example, new), which needs to store a large amount of data and keep it for a long time. It will be in the heap memory, and the application time will be longer. The free space will be used first, and the marked space will be reclaimed for use if it is not enough, and the expansion space will be applied for if it is not enough.

Stack memory is mainly used for temporary variables, such data does not occupy a lot of space such as constants, references, etc.

activation

The so-called activation refers to the part of memory that is called by the system. The memory of A is activated as soon as it is used for calculation, and then this memory will be allocated. On .

trigger mechanism

There are three situations when the GC mechanism is triggered, which are

1. When there is not enough space

2. Automatic trigger

3. Compulsory recycling

The first type of recycling when the space is not enough is because the allocated space is insufficient. When needed, the marked space will be reclaimed for allocating new object variables. Heap memory is more common, because heap memory often applies for more space. The order is 1, idle space 2, reclaiming marked memory 3, applying for expansion

The second type of automatic trigger is built-in. In order to maintain the smoothness of the program, the GC mechanism will patrol and reclaim the marked space by "randomly" GC.

The third way we can call the GC mechanism to recycle

GC operation

The operation of the GC mechanism mainly focuses on confirming whether the memory is being referenced, and the process is as follows

1. Check variables stored in memory

2. Confirm whether it is called

3. Non-references are marked as recyclable

4. Remove the marked variable and reclaim the occupied space to the memory

Summarize:

The GC mechanism is a bit like the destructor in C++, you need to apply for release by yourself, but in C# we don’t need to release it by ourselves, GC will help us release it, and remember that the GC mechanism will only recycle the data in the heap memory, and the data of value type will be released after being used in the stack. You may talk about pointers, but pointer operations in C# are not safe, and generally avoid using them, but it does not mean that they cannot be used. Unsafe code can be enabled .

Guess you like

Origin blog.csdn.net/hoxidohanabi/article/details/128099400