The CLR provides each AppDomain with a GC Handle Table that lets applications monitor or explicitly control object lifetimes. The table is empty when the AppDomain is created.
Each entry contains:
- A reference to an object on the managed heap
- A flag describing how to monitor/control that object
You typically interact with this via System.Runtime.InteropServices.GCHandle
(e.g., GCHandleType.Weak
, WeakTrackResurrection
, Normal
, Pinned
). Use cases include pinning objects for interop or creating weak references without allocating WeakReference
.
Example (pinning):
|
|
Example (weak):
|
|
Prefer high-level WeakReference
unless you specifically need GCHandle
semantics (e.g., pinning or resurrection tracking). Always Free()
to avoid leaks.