TinyIoC icon indicating copy to clipboard operation
TinyIoC copied to clipboard

Unnecessary SuppressFinalizes

Open benaadams opened this issue 7 years ago • 3 comments
trafficstars

Why do SafeDictionary and TinyIoCContainer both call GC.SuppressFinalize when neither have finalizers? 🤔

From https://github.com/NancyFx/Nancy/issues/2896

benaadams avatar May 23 '18 09:05 benaadams

I'm really not sure, were you able to get to the bottom of this?

niemyjski avatar Jul 30 '18 14:07 niemyjski

Either they used to have finalisers, they should have them and I forgot to add them, or its a mistake :)

grumpydev avatar Jul 30 '18 14:07 grumpydev

You dont have to have a finalizer to gain benefit from GC.SuppressFinalize When you call it, the object is removed from the finalizer queue. With it not in the finalizer queue, theres no wasted performance of having the object on the queue, fetching from the queue, and the GC having to reflect and figure out there's no finalizer to run.

The generally accepted pattern is objects that implement IDisposable call this, since IDisposable objects will get thrown into the finalizer queue whereas objects without IDisposable, I believe get GC'd practically immediately.

ericnewton76 avatar Dec 28 '18 20:12 ericnewton76