Net memory management


1. how to handle small objects?

small-scale.NETObjects are allocated to a small object heap(SOH)Up. There are 3 kinds: generation 0, generation 1 and generation 2. Objects move up according to their lifespan.

Place the new object on Gen 0. When Gen 0 is full Net garbage collector (GC) runs, processes objects that are no longer needed, and moves everything else toGen1。 If Gen 1 is full, the GC runs again, or you can move the objects in Gen 1 to Gen 2.

WhenGen 2When full, will occurGCFull operation. This will clear unwantedGen 2Objects that willGen 1Object move toGen 2, and thenGen 0Object is moved to Gen 1, and finally all unreferenced content is cleared. Each runGCAfter that, the affected heap is compressed to keep the memory still in use together.

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

be careful:If you areGen 2Seeing a large amount of memory in indicates that the memory has been reserved for a long time, and there may be a memory problem. This is where memory analysis tools come in handy.

2. what happens to larger objects?

greater than85 KBObjects of are allocated to the large object heap(LOH)。 Because of the overhead of copying large chunks of memory, they are not compressed. When a completeGCUnusedLOHThe address range of the object is recorded in the free space allocation table.

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

Since objects are unlikely to be the exact size of an empty address range, there is almost always a small amount of memory between objects, resulting in fragmentation. If these blocks are less than85 KB, there is no possibility of reuse at all. Therefore, as allocation requirements increase, new segments are retained even if debris space is still available.

In addition, when large objects need to be allocated Net still tends to append objects to the end rather than run expensiveGen 2 GC。 This is good for performance, but it is an important cause of memory fragmentation

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

. Net passed asGCMultiple modes are provided 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 be run as “concurrent” or “non concurrent”, which means runningGCThread of. The default is concurrency, which uses separate threads for GC, so applications canGCContinue execution while running.

Server mode can provide maximum throughput, scalability and performance for the server environment. In the server mode, the segment size and generation threshold are usually much larger than in the workstation mode, which reflects the higher requirements for the server.

The server mode runs garbage collection on multiple threads in parallel. Each logical processor is assigned a separate SOH and LOH to prevent threads from interfering with each other.

.NETThe framework provides a cross reference mechanism so that objects can still reference 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. insufficient references will compromise performance and memory efficiency

Weak object referencesGCAn alternative source for the root, allowing you to keep objects and collect them when GC needs them. They are a tradeoff between code performance and memory efficiency. Creating objects requiresCPUTime, but memory is required to maintain the loaded state.

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

5. object fixation can create references passed between managed and unmanaged code

.NETUsing a method calledGCHandleStructure to track heap objects.GCHandleCan be used to pass object references between managed and unmanaged domains Net maintenanceGCHandlesTable to achieve this.GCHandleThere are four types, including pinned, which are used to pin objects to specific addresses in memory.

The main problem with object fixation is that it can causeSOHFragmentation. If you pin an object toGCBy definition, the object cannot be relocated. Depending on the way you use fixity, it reduces the efficiency of compression and leaves gaps in the heap. The best strategy to avoid this is to lock for a short time and then release.

This is the end of this article about the five basics of net memory management. For more information about the five basics of net memory management, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you will support developeppaer in the future!

Recommended Today

Summary of Python object-oriented built-in functions

Python built-in functions 1. classmethod、staticmethod、property 。 The above three built-in functions have been described in detail in the methods and properties of the article (Python advanced – object-oriented members). You can return to browse. The specific address is: 2. callable, whether it can be executed with brackets. function def func(): pass print( callable(func) ) […]