Memory & GC
Reveal answer and reasoning
Choose the exact native resource and its release operation. Wrap it with a suitable SafeHandle implementation or an equivalent narrowly defined owner, then expose a clear Dispose boundary. Ensure partially constructed objects do not release uninitialized resources incorrectly and repeated disposal cannot release another owner's resource. If async operations are outstanding, define whether disposal waits, cancels and acknowledges, or rejects new operations while existing ones complete.
For an interview, contrast an object surviving because a cache roots it with an object retained for pending finalization. Explain why GC does not simply call Dispose on every unreachable object. Then describe a fallback that protects native resource release without making timely disposal optional. A strong answer keeps finalizer-thread behavior, native release correctness and managed memory reclamation as related but separate concerns.
Add an exception inside the portable using block and confirm disposal still happens. Record the exception separately and verify the release counter remains one after a repeated Dispose. Do not add a finalizer merely to make a deterministic unit test depend on collection timing.
Draw the wrapper and a large child through four states: ordinary root present, ordinary root absent but finalization pending, finalizer completed without resurrection, and a later collection. Explain what a weak observer might report only under its documented weak-reference policy; do not assume all weak-reference flavors have identical finalization behavior. Finish by listing evidence that would distinguish too many live resource owners from a blocked finalizer thread.
What a strong answer covers
- Finalization can retain an otherwise unreachable object graph until a later collection.
- GC does not automatically call IDisposable.Dispose.
- Resurrection creates a new ownership path and complicates reasoning.
Common pitfall
Assuming the GC invokes Dispose, or treating finalizer completion as immediate memory reclamation.
Follow-up questions
- What new path does resurrection introduce?
- How would a blocked finalizer affect unrelated pending cleanup?
Self-review
Choose what to revisit. These marks are your own assessment and are separate from lesson completion.