Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of garbage collection mechanism in c#

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces what is the principle of garbage collection mechanism in c#. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Write a window service, update the sqlite record in a loop, and the memory grows steadily. Three days later, memory overflowed. So I started with my own code to find out where the memory could not be released, and it was clear that it was caused by calling servicestack.ormlite to update the sqlite database. As for whether it is a framework problem, it is unlikely, because there is nothing wrong with the code executed by the local simulation. I think it is due to the fact that the object is still being referenced after orm performs the database update. Here, I post a pseudo code:

Is my guess right in the end? I don't know yet. However, in the search for answers, I have a detailed understanding of the relevant mechanisms of GC.

The picture is from ".NET Advanced debugging" pdf.

When the CLR loads, the heap is allocated.

III. The working mechanism of GC

GC has three assumptions:

1. If there is no special declaration, all objects are garbage (track whether the object is garbage by reference)

2. Assume that the active time of all objects on the managed heap is short (GC will collect transient active objects more frequently than long-term active objects)

3. Track the duration of the object by generation

The following is the official document that is consistent with these three assumptions

The garbage collector in the common language runtime supports object aging using generations

Objects created more recently are part of newer generations, and have lower generation numbers than objects created earlier in the application life cycle.

Objects in the most recent generation are in generation 0. This implementation of the garbage collector supports three generations of objects, generations 0, 1, and 2

Each generation has its own heap. If the generation 0 heap is full, it will trigger GC, and then upgrade the objects that are still referenced to the first generation object. Finally, compress the heap and merge the remaining heap space into one. The same is true of first-generation objects. But when it comes to the second generation, the treatment is different. The second-generation heap may be a large object heap, which is too expensive to compress, so it just merges adjacent space.

Photo source blog Park c# technology talk about garbage collection (GC)

Garbage collection happens automatically when a request for memory cannot be satisfied using available free memory

The timing of GC occurs when the corresponding heap reaches the threshold, because the heap also has a size limit and is not infinite. Although when the second generation heap or large objects are full, a new memory segment is added to meet the memory allocation, if no memory is available, a memory overflow will be reported.

4. GC cannot release unmanaged resources

There are two cases, the first: managed code references unmanaged resources, such as file operations, database connections, network connections, and so on. At this point, you must manually release, or implement the dispose pattern, or implement the object termination

When a type uses unmanaged resources that must be released before instances of the type are reclaimed, the type can implement a finalizer.

In most cases, finalizers are implemented by overriding the Object.Finalize method; however, types written in C# or C++ implement destructors, which compilers turn into an override of Object.Finalize

It is important to note that the object Terminator is implemented and GC is called automatically before the object is released. In fact, this is a very expensive backup mechanism. So those who can release unmanaged resources by themselves will release them by themselves.

If an object contains a Terminator, it is placed in the Terminator queue when new. When GC marks the object as garbage, it is placed in another queue, F-Reachable. This queue contains all objects with finalizers that will be garbage collected, and finalizers for these objects will be executed. Finalizer code is not always executed during garbage collection. Instead, it is called by the terminating thread of the .NET process. Therefore, garbage collection lags for a period of time in order to wait for the finalizer code execution to be completed.

5. Dispose model

On the principle of garbage collection mechanism in c # is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report