Five Fundamentals of Net Memory Management

  • 2021-12-04 09:55:37
  • OfStack

Directory 1. How to deal with small objects? 2. What happens to larger objects? 3. The garbage collector can run in different modes to optimize performance 4. Insufficient references tradeoff between performance and memory efficiency 5. Object fixation can create references passed between managed and unmanaged code

1. How to deal with small objects?

Small .NET Objects are assigned to a small object heap ( SOH ). There are three species: generation 0, generation 1 and generation 2. Objects move upward according to their lifetime.

Place the new object on Gen 0. When Gen 0 is full, the. NET garbage collector (GC) runs, processing objects that are no longer needed, and moving everything else to the Gen1 . If Gen 1 is full, GC runs again, and objects in Gen 1 can also be moved to Gen 2.

When Gen 2 When it becomes full, it will occur GC Fully operational. This will clear the unwanted Gen 2 Object, setting the Gen 1 Object to the Gen 2 And then set the Gen 0 Object to Gen 1, finally clearing all unreferenced content. Every run GC After that, the affected heap is compressed to keep the memory still in use at 1.

This intergenerational approach ensures that things run efficiently-the time-consuming compression process only happens when absolutely necessary.

Note: If you are in the Gen 2 If you see a large amount of memory in, it indicates that the memory has been reserved for a long time and there may be memory problems. This is where memory analysis tools can come in handy.

2. What happens to larger objects?

Greater than 85 KB Objects of are assigned to the large object heap ( LOH ). Because of the overhead of copying large chunks of memory, they are not compressed. When a complete GC Unused LOH The address range of the object is recorded in the free space allocation table.

After a new object is allocated, the address range sufficient to accommodate the object is checked in this free space table. If it exists, the object is allocated there, and if it does not exist, the object is allocated to the next 1 available space.

Because objects are unlikely to be the exact size of an empty address range, small chunks of memory are almost always left between objects, resulting in fragmentation. If these blocks are less than 85 KB There is no possibility of reuse at all. Therefore, as allocation requirements increase, new segments are retained even if fragmentation space is still available.

In addition, when large objects need to be allocated,. NET still tends to attach objects to the end instead of running expensive ones Gen 2 GC . This is good for performance, but it is an important cause of memory fragmentation

3. The garbage collector can run in different modes to optimize performance

. NET through GC Provide multiple modes to solve the trade-off between performance and heap efficiency.

Workstation mode provides users with maximum response speed and reduces pauses due to GC. It can run as "concurrent" or "non-concurrent", meaning running GC The thread of the. The default value is concurrent, which uses a separate thread for GC, so the application can use the GC Runtime continues to execute.

Server mode provides maximum throughput, scalability, and performance for a server environment. In server mode, segment sizes and build thresholds are typically much larger than in workstation mode, reflecting higher requirements for the server.

Server mode runs garbage collection in parallel on multiple threads, allocating a separate SOH and LOH to each logical processor to prevent threads from interfering with each other.

.NET The framework provides a cross-reference mechanism, so objects can still refer to each other between heaps. However, since application responsiveness is not a direct target of server mode, all application threads will be suspended during GC.

4. Under-reference tradeoff between performance and memory efficiency

Weak objects refer to GC An alternative source to the root, allowing you to preserve objects and collect them when GC requires them. They are a trade-off between code performance and memory efficiency. Creating an object requires CPU Time, but it takes up memory to keep it loaded.

Weak references are especially suitable for large data structures. For example, suppose you have an application that allows users to browse through large data structures, and they might return some of that data. You can convert any strong reference into a weak reference to the structure they browse for. If the user returns to these structures, they can be used, but if not, GC can recycle memory as needed.

5. Object fixation creates references to pass between managed and unmanaged code

.NET Use a type called GCHandle To trace heap objects. GCHandle Can be used to pass object references between managed and unmanaged domains. NET maintains 1 GCHandles Table for this purpose. GCHandle There are four types, including fixed ones, which are used to fix objects to specific addresses in memory.

The main problem with object fixation is that it can lead to SOH Fragmentation. If the object is fixed in the GC By definition, the object cannot be relocated. Depending on how you use fixation, it will reduce the efficiency of compression and leave a gap in the heap. The best strategy to avoid this situation is to lock in for a short time and then release.

Related articles: