Some questions that may help you understand the theme:
There are some classes that have "features" that go beyond the content of the managed memory. In these cases the class itself must take some action of how to proceed the destruction of its content. It needs a finalizer method. This is very common in implementing the layout pattern (interface IDisposable
). The finisher is useful in all situations that the destruction of the object must be customized and goes beyond the displacement of the memory, that only the GC can proceed, however you want.
This is a way to indicate to the GC that it should not call the finalizer, since it was called by the class at an earlier time. If it is called, it will attempt to perform a cleanup that has already been performed and probably some problem will occur.
Example of implementation:
public class MyClass : IDisposable {
private bool disposed = false;
protected virtual void Dispose(bool disposing) {
if (!disposed) {
if (disposing) {
// chamado pelo myClass.Dispose().
// OK usar quaisquer referências para objetos privados
}
disposed = true;
}
}
public void Dispose() { // necessário para a interface IDisposable
Dispose(true);
GC.SuppressFinalize(this);
}
~MyClass() { //método finalizador (destrutor)
Dispose(false);
}
}
I put in the Github for future reference.
The provision should only be used in specific cases where it is really necessary. One cannot be tempted to force the release of resources when only managed memory is allocated. This does not work. The memory release can only be performed by the GC. The method GC.SuppressFinalize()
only informs that the completion has already been accomplished, not that the memory release has occurred. Nor could it do this. The memory allocation of the CLR is done in an entirely own way and in an integrated way, the application has no control over it. You can’t control a part of the allocation. It’s one thing.
For those who do not know well what to do in these cases, it is better to follow the recipe above. But it would be good to study the subject thoroughly before creating a class that relies on external resources. Most applications don’t need this. Just consume what already exists.
There is no problem of performance in using it. Unless the specific algorithm used in the finalizer has some performance problem of its own, but it will not be the fault of this GC method.
Other OS question on the subject.
I imagine you essentially want that excellent answer. Do you know English? Do you know that you can post the answer yourself? Do you want to do this? Or would you rather have someone post here?
– Maniero
I do not intend to answer at first, only if someone does not answer for a long time (1 week).
– rubStackOverflow