Home  >  Article  >  Backend Development  >  .Net garbage collection mechanism detailed introduction

.Net garbage collection mechanism detailed introduction

高洛峰
高洛峰Original
2016-12-20 14:13:331734browse

Destructor

The destructor 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

.NET Garbage Collector will guarantee:

l Every object will be destroyed and its destructor will definitely be run. When a program ends, all objects are destroyed.
l Each object is destroyed only once.
l Each object will only be destroyed when it is unreachable (that is, when there is no reference to the object).

How it works:

1) It constructs a map containing all reachable objects. To do this, it repeatedly follows the reference fields in the object. The garbage collector will construct this map very carefully and ensure that cyclic references do not recur infinitely. No object in this map will be considered unreachable.
2) It checks if any unreachable object has a destructor that needs to be run (the process of running a destructor is called finalization). Any unreachable objects that require finalization are placed in a special queue. This queue is called the freachable queue.
3) It recycles the remaining unreachable objects (that is, objects that do not require finalization). To do this, it moves reachable objects down in the heap, thereby defragmenting the heap and freeing the memory located at the top of the heap. When the garbage collector moves a reachable object, it also updates the reference to the object.
4) It then allows other threads to resume execution
5) It performs the finalize operation on unreachable objects (located 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 cause the above process to execute two more steps, 2 and 5. So consider using using blocks instead of generics. If a class used implements the Dispose method (Close method). It is best to call this method in finally (before calling the method, you need to check whether the disposed attribute of the object to be disposed is false, and only dispose if it is not true. This is also the reason why using is recommended. using can easily constrain this. The scope of the variable to be destructed - that is, between a pair of braces). Or use a using block to surround the code that uses this class. The type of 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, sample code:

class MyClass : IDisposable
{
  private bool disposed = false;//Disposal 状态
 
  public void Dispose()//公有Dispose方法(可选实现IDisposal接口)
  {
    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 Dispose execution. , In addition, GC.SuppressFinalize(this); is used to prevent the compiler from performing destruction on this object.

Thanks for reading, I hope it can help everyone, thank you for your support to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn