
Resource cleanup in .NET is one of those topics that looks simple until you start dealing with native handles, unmanaged memory, sockets, streams, or long-running services.
Most of the time, the answer is straightforward: use IDisposable, wrap cleanup in using, and keep ownership clear.
But sometimes you need more.
You may need finalization as a last-resort safety net. You may need SafeHandle for native OS resources. You may need IAsyncDisposable when cleanup itself involves asynchronous work.
This article breaks down how finalization, IDisposable, and IAsyncDisposable actually fit together, when to use each one, and what mistakes to avoid.
A finalizer in C# looks like this:

A finalizer is a last-chance safeguard for unmanaged resources.
When an object with a finalizer becomes unreachable, the garbage collector does not immediately reclaim it. Instead, the object is moved to the finalization queue. Later, a dedicated finalizer thread runs the finalizer. Only after that can the object be collected.
That means finalization is not deterministic cleanup. It is delayed cleanup.
And that distinction matters.
Finalizers should be rare. They introduce several important runtime implications.
First, finalizers are nondeterministic. You do not control when they run. In some shutdown scenarios, you may not be able to rely on them running at all.
Second, finalizers delay collection. An object with a finalizer usually survives longer than a normal object. If that object references a large object graph, the entire graph may stay alive longer than expected.
Third, finalizers run on a dedicated finalizer thread. Expensive or blocking work in a finalizer can delay cleanup for every other object waiting to be finalized.
Fourth, there are no ordering guarantees. You should not assume other managed objects are still valid or usable from a finalizer. They may already have been finalized.
Fifth, finalizers must never throw. An unhandled exception on the finalizer thread can be process-fatal.
And finally, object resurrection is a trap. Making this reachable again from a finalizer, for example by assigning it to a static field, complicates object lifetime and often creates leaks.
The golden rule is simple:
A finalizer should touch only unmanaged state directly owned by the object, and it should do the absolute minimum required to release that state.
If an object has a finalizer but the resource was already cleaned up deterministically, call:

This tells the runtime that finalization is no longer needed for that object.
Even if your type does not currently have a finalizer, calling GC.SuppressFinalize(this) inside Dispose() is harmless. Many teams include it consistently because it keeps the pattern future-proof.
If your object owns an operating system handle or a similar native resource, prefer SafeHandle.
Examples include:

You can also create your own SafeHandle subclass for custom native handles.
SafeHandle is usually better than writing a raw finalizer because it encapsulates native release logic in a reliable finalization mechanism. It also works well with P/Invoke and helps prevent double-free bugs and lifetime issues.
The design heuristic is:
If the resource can be wrapped in SafeHandle, wrap it in SafeHandle and avoid writing your own finalizer.
Then your higher-level type can simply implement IDisposable and dispose the handle deterministically.
IDisposable gives callers a clear contract for explicit cleanup.
Consumers can write:

or:

This makes cleanup deterministic. The resource is released when the scope exits.
For most application and library code, this is the preferred model.
When a class is sealed and owns disposable managed resources, the implementation can stay simple.

This is the kind of cleanup code you should prefer whenever possible.
The class has no finalizer. The native handle is owned by SafeFileHandle, and the class disposes it deterministically.
Sometimes a type directly owns unmanaged memory or a native resource that cannot be wrapped in SafeHandle.
In that case, a finalizer may be justified as a last-chance safety net.

The important distinction is the disposing parameter.
When disposing == true, cleanup was triggered by Dispose(), so it is safe to dispose owned managed fields.
When disposing == false, cleanup was triggered by the finalizer, so the code should avoid touching managed objects and release only unmanaged state.
A good Dispose() implementation should be idempotent. Calling it multiple times should be safe.
It should also be cheap. Check a _disposed flag early and return quickly.
If multiple threads may call Dispose() at the same time, protect the cleanup path. For example, use Interlocked.Exchange on a state field to make sure cleanup executes only once.
Also remember to guard public methods after disposal:

A disposed object should not continue operating as if it were still valid.
Some resources cannot be cleaned up efficiently using only synchronous disposal.
Examples include:
For those cases, implement IAsyncDisposable.
Consumers can then write:

A simple implementation looks like this:

Use IAsyncDisposable when cleanup itself needs asynchronous work.
Do not hide asynchronous I/O behind a synchronous Dispose() unless you have thought carefully about blocking, deadlocks, and latency.
Library types often implement both for flexibility.
This allows consumers to choose:

or:

If you implement both, try to avoid duplicate cleanup logic. A single shared code path reduces drift and bugs.
In synchronous Dispose(), some code calls:

This can be acceptable in console applications or services where there is no problematic synchronization context. But it can be risky in legacy UI or ASP.NET-style synchronization contexts because sync-over-async can deadlock.
Document the recommended usage clearly.
For example:
Prefer await using for best cleanup latency.
Keep DisposeAsync() non-cancelable. Once cleanup starts, it should complete.
Avoid doing sync-over-async inside the type unless you have measured the cost and accepted the tradeoff.
Use ValueTask as the return type, as required by IAsyncDisposable.
And just like Dispose(), make DisposeAsync() idempotent. Multiple calls should not cause duplicate cleanup or double-free behavior.
Here are the rules of thumb I use.
Prefer no finalizer, IDisposable, and SafeHandle for OS handles and most interop scenarios.
Use IAsyncDisposable when cleanup involves async I/O, such as network flushes, pipe drains, or asynchronous stream completion.
Use using and await using at the call site to make ownership clear and cleanup deterministic.
Use a finalizer only when the type directly owns unmanaged memory or a native resource that cannot reasonably be wrapped in SafeHandle, and you need a last-resort release mechanism in case callers forget to call Dispose().
Avoid doing meaningful work in a finalizer. Avoid touching other managed objects from a finalizer. Avoid throwing from finalizers. Avoid long-running or blocking operations in finalizers. Avoid resurrecting objects.
Finalizers are a safety net, not a resource management strategy.
The garbage collector understands managed memory. It does not automatically understand the size of unmanaged allocations owned by managed objects.
If your managed object owns significant native memory, such as large image buffers, compression dictionaries, native caches, or GPU staging areas, consider using:

when the native memory is allocated, and:

when it is released.
This does not force a garbage collection. Instead, it adjusts the GC’s internal budgeting so collections happen at more appropriate times.
Use it for large or long-lived native allocations. Do not call it for tiny buffers or very high-churn allocations where the overhead may outweigh the benefit.
Always pair AddMemoryPressure and RemoveMemoryPressure. If allocation can fail partway through construction, use exception-safe cleanup so the calls stay balanced.
If you are consuming disposable types:
The best resource cleanup code is usually boring.
Most types should not have finalizers. Most native handles should be wrapped in SafeHandle. Most consumers should rely on using or await using.
Finalization exists for last-resort cleanup, not as a primary design tool.
When ownership is explicit and cleanup is deterministic, your code becomes easier to reason about, easier to test, and much safer in production.