To fix some confusion:
You only need a finalizer if you need to clean up unmanaged resources in a special way.
If you do not have unmanaged resources, you do not need a finalizer.
If you have a finalizer, you should definitely implement IDisposable.
If you have a finalizer, you should call GC.SuppressFinalize in Dispose, because the finalizer does not need to be called if you have already cleared after yourself.
You only need to call GC.SuppressFinalize if you have a finalizer. If you do not, you probably still want to call it a protective measure.
suppresses the GC finalizer here, saves time for the GC?
If there are unmanaged resources, and your object has a finalizer, then yes; your object may be in the finalization queue for a long time if you do not suppress it. The resources you have can also take a long time to complete (for example, closing an expensive I / O channel).
If you donβt have a finalizer, you say that you donβt have anything unmanageable to clean up. You should still call SuppressFinalize a protective measure anyway.
From the rules of FxCop:
"The inability to suppress finalization reduces performance and offers no benefits."
For your other question,
Could this cause a memory leak?
In this example, yes, if any of the resources is unmanageable. If your object ends in some unusual way before it can call Dispose , unmanaged resources will not be properly freed. Consider what happens if someResource unmanaged and throws an exception, for example when using an instance of someObject .
If your object rests on unmanaged resources, and you need to ensure that these resources are cleared, you also need a finalizer (which will be called ~someObject in your case).
source share