DOTNET Learning Notes Four - Unused Unit Recycling

zhaozj2021-02-11  211

We usually write programs rarely write resource management, unless you write very large applications, or your company's own SDK. I have seen the PGP source code must know that the PGP's SDK has implemented its own memory management. It is really difficult to manage memory troubles. I forgot to release it, and the bugs that have been released again and again, this BUG is very difficult. Ordinary logic bug, the simple test discovery program can be found in accordance with the expected operation. But the problem of memory is hard to find. In the past, many companies have also made great efforts to solve this problem, such as Compware's BoundSchecker, Rational's Purify. These tools are also very difficult, and often found points are the code you use the development library. Now .NET provides a full set of resource management (free resource recycling: Garbage Collection, referred to as GC), can let us free from it, use the energy problem that you should solve in your own business. Of course it is not a million, we are best to understand his principles so that we can enjoy it better.

System resources are not unlimited, memory is intended to be released, files, and network connections are system resources, and they must be released after running. In an object-oriented system, all things are objects, so use any resources to allocate memory from the system, and finally release it. There is no extra five steps using resources.

1. Assign memory to the type of resource;

2. Initialize the state of the resource, request non-memory system resources (such as opening the file, establishing a network connection, etc.);

3. Access resources by accessing instances (objects) and its member variables, methods, etc. (maybe multiple times)

4. Empty resource status, release system resources (such as turning off files, shutting down network connections);

5. Release memory

The memory problems we have encountered generally in five steps above, the NET provided by the .NET is basically solved these problems basically. However, GC does not know how to empty the resource status and release system resources (ie, the fourth step above), this is to use to use Fina

li

ZE method, this will be discussed later. Of course, most of the objects, such as strings, etc., no need this fourth step.

The CLR implements a managed heap that requires all resources to be allocated from this stack and do not need to be released. The following detailed explanation of how the object is allocated in the heap, and how the GC does not use the unused unit recycling.

When the CLR is initialized, a continuous memory is retained. This continuous memory is a hosted stack. The CLR also maintains a pointer for the hosted stack, which always points to the next memory space, and we call nextobjptr.

When the program applies to NEW to create an object, NEW first confirms if there is enough memory space in the heap, if any, the object assignment space, call the object's constructor, and returns the address of the assignment space, then NEXTOBJPTR points to the remaining space Address, the next address can be assigned. As shown below:

In the figure, the dashed line is the start address of NextObjPtr, and the NEXTOBJPTR is moved to the solid line.

Let us look at the pluck memory allocation method of ordinary applications. Ordinary memory allocation maintains an idle memory list, the system first passes the idle space list, find a sufficient space, then split enough space allocation, then add the remaining space to the free space list. There are many algorithms that implement the process stack in history, such as more famous two-way methods. But in comparison, .NET's memory allocation method is much faster. However, what should I do if my memory is not unlimited? When the CLR is allocated, if the idle space in the heap is found, the useless space recycling will be started. GC releases the memory release of the object that is no longer used in the heap, then organizes the heap, so that the remaining space is to be allocated, of course, if there is no release, the memory is still not enough, then throws OutofMemoryException is abnormal. How does the GC determine an object no longer used?

Each application has a set of roots that include storage units that mark a set of objects in the hosted stack. Objects being considered to be roots include:

All global and static objects;

2. Parameters of partial objects and functions in a line in stack;

3. Any object that is pointed to the pointer to any CPU register;

The list of roots is maintained by JIT and CLR and GC can be accessed.

Start the use of the unused unit to recycle the GC to start traversing the root, find the object pointing to the first root, looking down the object, find the object all references to this object, and the referenced reference, and put it into a collection . After this is complete, you will then find the next root. Once the GC discovers that an object is already in the collection, the search will stop this branch to prevent repetitive and reference dead cycles.

After completing, GC has a collection of objects that all the roots can access, and the objects in this collection are considered to be useless. As shown below:

GC's collection includes objects A, B, D, F, and objects C, E, G are useless objects, and GC will release its resources. The GC then traverses all objects in the heap, releases unused objects, and moves the useful object to the position (which is said to be used) to ensure continuity of idle space. NextObjPTR is directed to the start address of idle space. This will make some object's reference address failed, and GC is responsible for correcting these pointers. The pile of recovery is as shown below:

The work made by this recycling is not more complex, and the CPU time consumed is also a lot. But fortunate, it is not always running, but only after the stack is full (actually the generation is full, Generation I will discuss the next article), other assignment memory is still Very fast. And .NET provides rich settings to increase the efficiency of the recycling of unused units.

I will discuss Fina in the next section.

li

ZATION and Generation, strong reference, weak reference.

转载请注明原文地址:https://www.9cbs.com/read-5801.html

New Post(0)