Finalizing and Disposing

Finalization allows an object to release unmanaged resources it’s using before it’s collected. The .NET Object class defines the following method signature for this purpose:

protected override void Finalize();

During a collection run, the GC places all unreferenced objects in a finalize queue. This queue is then scanned, and the memory occupied by classes that don’t define a Finalize method is released. Objects that do define a Finalize method are placed on the freachable (pronounced F-Reachable) queue. A dedicated thread responsible for calling Finalize methods sleeps when no objects are in the freachable queue. When objects are added to the queue, the thread is awakened and calls the Finalize method.

Caution

A dedicated ...

Get C# for Java Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.