Details of. Net garbage collection mechanism

  • 2021-08-28 19:46:49
  • OfStack

Destructor

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

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


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

Garbage collector

. NET the garbage collector guarantees that:

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

Working mode:

1) It constructs an map containing all reachable objects. To do this, it repeatedly follows the reference fields in the object. The garbage collector constructs this map very carefully and ensures that circular references are not infinitely recursive. Any object in this map will not be considered unreachable.
2) It checks whether any unreachable objects have a destructor to run (the process of running the destructor is called finalization). Any unreachable objects that require finalization are placed in a special queue. This queue is called freachable queue.
3) It recycles the remaining unreachable objects (that is, objects that do not require finalization). To do this, it defragments the heap by moving reachable objects down in the heap and frees the memory at the top of the heap. When the garbage collector moves 1 reachable object, it also updates the reference to the object.
4) It then 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 destructor will make the above process perform 2 and 5 more steps. So consider using using blocks instead of generics. If one of the classes used implements the Dispose method (Close method). It is best to call this method in finally (before calling the method, check whether the disposed attribute of the object to be dispose is false, and only then dispose when it is not true, which is why using is recommended, and 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 the code using this class. The type of object placed in the using block must implement the IDisposable interface.

Standard cleaning mode

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


class MyClass : IDisposable
{
  private bool disposed = false;//Disposal  Status 

  public void Dispose()// Public ownership Dispose Method (optional implementation 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 Dispose executes. In addition, GC. SuppressFinalize (this); Used to prevent the compiler from performing destructions on this object.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: