ReRegisterForFinalize SuppressFinalize real-life example

I just read this article, “ Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework, ” by Jeffrey Richter, and I could not come up with any lifestyle for using ReRegisterForFinalize or SuppressFinalize .

Can someone provide me some examples?

+6
source share
3 answers

The few places he uses the .NET platform are always a good place to view. Basic templates:

  • The deleted object is reused. The Dispose () method called SuppressFinalize, so you need to reregister it (NativeWindow, RequestContextBase, TaskExceptionHolder class).
  • the finalizer failed and found an exception. Little to do but retry later. This code is wrapped if (! Environment.HasShutdownStarted & &!! AppDomain.CurrentDomain.IsFinalizingForUnload ()) to make sure it makes sense (class DynamicResolver and LoaderAllocatorScout)
  • the object participates in the caching scheme and receives re-caching (class OverlappedData)
+9
source

The implementation of IDisposable often requires SuppressFinalize : look here or here for the code.

Now I do not have a good example on ReRegisterForFinalize .

+3
source

You need to ReRegisterForFinalize when resurrecting an instance. Resurrection (mentioned in a related article) is the action to restore the root of an object from its destructor (finalizer).

This only translates the question to the question "when will you resurrect the object." In my answer to this question, I suggested that this could use a connectionpool or similar design.

+1
source

Source: https://habr.com/ru/post/899875/


All Articles