In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "what are the basics of Net memory management". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the basics of Net memory management.
Catalogue
1. What do you do with the little object?
two。 What happens to larger objects?
3. Garbage collectors can run in different modes to optimize performance
4. Insufficient references make a tradeoff between performance and memory efficiency
5. Object anchoring can create references passed between managed and unmanaged code
1. What do you do with the little object?
Small .NET objects are assigned to a small object heap (SOH). There are three kinds of them: the 0th generation, the first generation and the second generation. Objects move up according to their lifespan.
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 Gen1. If Gen 1 is full, GC runs again, or objects from Gen 1 can be moved to Gen 2.
When Gen 2 becomes full, a full run of GC occurs. This clears unwanted Gen 2 objects, moves the Gen 1 object to Gen 2, then moves the Gen 0 object to Gen 1, and finally clears all unreferenced content. Each time you run GC, the affected heap is compressed to keep the memory still in use together.
This intergenerational approach ensures that things run efficiently-a time-consuming compression process that occurs only when absolutely necessary.
Note: if you see a large amount of memory in Gen 2, it indicates that the memory has been retained for a long time and there may be memory problems. This is where memory analysis tools can come in handy.
two。 What happens to larger objects?
Objects larger than 85 KB are assigned to the large object heap (LOH). They are not compressed because of the overhead of copying large chunks of memory. When a full GC occurs, the address range of unused LOH objects is recorded in the free space allocation table.
After a new object is assigned, the address range sufficient to accommodate the object is checked in this free space table. If it exists, assign the object there, and if it does not exist, assign the object to the next available space.
Because objects are unlikely to be the exact size of an empty address range, there are almost always small chunks of memory left between objects, resulting in fragmentation. If these blocks are less than 85 KB, there is no possibility of reuse at all. Therefore, as the allocation requirement increases, new segments are retained even if the fragment space is still available.
In addition, .NET still tends to append objects to the end when large objects need to be allocated, rather than running the expensive Gen 2 GC. This is good for performance, but it is an important cause of memory fragmentation
3. Garbage collectors can run in different modes to optimize performance
.net solves the tradeoff between performance and heap efficiency by providing multiple modes for GC.
Workstation mode provides maximum response time for users and reduces pauses caused by GC. It can be run as "concurrent" or "non-concurrent", referring to the thread running GC. The default value is concurrency, which uses separate threads for GC, so the application can continue execution while GC is running.
Server mode provides maximum throughput, scalability, and performance for the server environment. In server mode, segment size and generation threshold are usually much larger than workstation mode, which reflects higher requirements for servers.
The 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.
The .NET Framework provides a cross-reference mechanism, so objects can still reference each other between heaps. However, because application responsiveness is not a direct target of the server pattern, all application threads will be suspended during GC.
4. Insufficient references make a tradeoff between performance and memory efficiency
Weak objects refer to an alternative source of the GC root, allowing you to retain objects while collecting them when GC needs them. They are a tradeoff between code performance and memory efficiency. Creating an object takes CPU time, but keeping it loaded takes up memory.
Weak references are especially suitable for large data structures. For example, suppose you have an application that allows users to browse large data structures, and they might return some of that data. You can convert any strong reference to a weak reference in the structure they browse. If the user returns to these structures, they can be used, but if not, GC can reclaim memory as needed.
5. Object anchoring can create references passed between managed and unmanaged code
.net uses a structure called GCHandle to track heap objects. GCHandle can be used to pass object references between managed and unmanaged domains, and .NET maintains a GCHandles table for this purpose. There are four types of GCHandle, including pinned ones, used to pin objects to specific addresses in memory.
The main problem with object pinning is that it can cause SOH fragmentation. If an object is pinned during GC, it cannot be repositioned by definition. Depending on the way you use it, it reduces the efficiency of compression, leaving gaps in the heap. The best strategy to avoid this is to lock it in a very short period of time and then release it.
At this point, I believe you have a deeper understanding of "what are the basics of Net memory management?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.