You are developing an application by using C#.
The application includes an object that performs a long running process.
You need to ensure that the garbage collector does not release the object’s resources until the process
completes.
Which garbage collector method should you use?
A.
ReRegisterForFinalize()
B.
SuppressFinalize()
C.
Collect ()
D.
WaitForFullGCApproach()
B suppressFinalize
KeepAlive() is the correct function. it prevents the disposal of an object in a long running process, only needed if you are using unmanaged code.
SupressFinalize is just something that provides an optimization that allows the system to not bother queing the object to the finalizer thread. A properly written Dispose()/finalizer should work properly with or without a call to GC.SupressFinalize().
I confirm that we should use KeepAlive() function in order to extend the lifetime of an object. SuppressFinalize prevent the disposing of an object twice and has nothing to do with long running processes.
I agree. See https://stackoverflow.com/questions/7295195/how-to-use-gc-keepalive-and-for-what-purpose
B.
by calling SuppressFinalize, the object is placed in a list of objects that are marked as ready for finalization. The garbage collector calls the Finalize methods for the objects in this list and removes the entries from the list. This method blocks until all finalizers have run to completion.