Garbage Collector responsibilities: which items are NOT performed by the GC in .NET?

Difficulty: Easy

Correct Answer: 1, 4, 5

Explanation:


Introduction / Context:
The .NET Garbage Collector (GC) manages memory on the managed heap by reclaiming objects that are no longer reachable. However, some resource cleanup tasks lie outside its direct responsibilities and must be handled explicitly or via disposable patterns.



Given Data / Assumptions:
The items are:

  • 1) Freeing memory on the stack.
  • 2) Avoiding memory leaks.
  • 3) Freeing memory occupied by unreferenced objects.
  • 4) Closing unclosed database connections/collections.
  • 5) Closing unclosed files.


Concept / Approach:
The stack is automatically managed by the runtime and CPU, not by the GC, so 1 is NOT a GC task. The GC does free memory for unreachable objects (3), helping to prevent many memory leaks, but it does not automatically dispose external resources like file handles or database connections (4 and 5). Those must be released via Dispose/using patterns or by safe handles/finalizers.



Step-by-Step Solution:
Evaluate 1 → Not GC: the stack is managed outside the GC.Evaluate 2 → Partially true benefit: GC reduces leaks by reclaiming unreachable objects, but the prompt asks what is NOT performed; 2 is not selected as NOT performed.Evaluate 3 → GC performs this; do not mark as NOT.Evaluate 4 → Not GC: requires Dispose or finalizers/handles.Evaluate 5 → Not GC: external resource release is programmer-controlled.


Verification / Alternative check:
Best practices recommend IDisposable and using statements for deterministic cleanup of unmanaged resources; GC finalization is non-deterministic.



Why Other Options Are Wrong:
Any option that tags freeing unreachable objects as NOT performed misrepresents the GC’s core duty.



Common Pitfalls:
Relying on finalizers for database connections or files leads to resource exhaustion; always dispose deterministically.



Final Answer:
1, 4, 5

More Questions from .NET Framework

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion