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

Detailed introduction of garbage collection mechanism of .net

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "A detailed introduction to the garbage collection mechanism of .net". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Destructor function

Destructors cannot have modifiers, such as public. Cannot accept any parameters.

The compiler automatically converts a destructor into an override version of the Object.Finalize method, as follows.

Class Test {protected override void Finalize () {try {… } finally {base.Finalize ();}

Garbage collector

The .NET garbage collector guarantees:

Every object will be destroyed and its destructor must be run. When a program ends, all objects are destroyed.

Each object is destroyed only once.

Each object is destroyed only when it is unreachable (that is, when a reference to the object does not exist).

Mode of work:

1) it constructs a map that contains all reachable objects. To do this, it repeatedly follows the reference field in the object. The garbage collector constructs the map very carefully and ensures that circular references are not infinitely recursive. No object in this map will be considered unreachable.

2) it checks whether any unreachable objects have a destructor that needs to be run (the process of running the destructor is called finalization). Any unreachable objects that need finalization will be placed in a special queue. This queue is called the freachable queue.

3) it collects the remaining unreachable objects (that is, objects that do not need finalization). To do this, it moves reachable objects down in the heap, defragmenting the heap and freeing memory on top of the heap. When the garbage collector moves a reachable object, the reference to that object is also updated.

4) then, it allows other threads to resume execution

5) it performs finalize operations on unreachable objects (in the freachable queue) that require finalization in a separate thread.

As can be seen from the above summary, the existence of the destructor will make the above process perform two more steps. So consider using using blocks instead of generics. If you are using a class that implements the Dispose method (the Close method). It is best to call this method in finally (check whether the disposed property of the object to be dispose is false before calling the method, and dispose only if it is not true, which is why using is recommended. Using can easily constrain the scope of the variable to be destructed-that is, between a pair of curly braces). Or use the using block to surround it with the code of this class. The type of the object placed in the using block must implement the IDisposable interface.

Standard cleanup mode

Finally, a standard cleanup mode code recommended by .NET is given, and the sample code is:

Class MyClass: IDisposable {private bool disposed = false;//Disposal status public void Dispose () / / Public Dispose method (optional implement IDisposal interface) {Dispose (true); GC.SuppressFinalize (this);} ~ MyClass () {Dispose (false);} protected virtual void Dispose (bool disposing) {if (! disposed) {if (disposing) {/ / Dispose the managed resources. } / / Dispose the unmanaged resources. } disposed = true;}}

In the above code, we call the Dispose method from the destructor, which ensures that the Dispose executes. And GC.SuppressFinalize (this); is used to prevent the compiler from performing destructions on this object

This is the end of "A detailed introduction to the garbage collection mechanism of .net". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report