Bird
0
0

Why is it generally discouraged to rely on finalize() for resource cleanup in Java?

hard📝 Conceptual Q10 of 15
Java - Memory Management Basics
Why is it generally discouraged to rely on finalize() for resource cleanup in Java?
ABecause finalize() automatically frees all memory instantly
BBecause finalize() is called immediately after object creation
CBecause finalize() execution timing is unpredictable and may delay cleanup
DBecause finalize() must be called manually by the programmer
Step-by-Step Solution
Solution:
  1. Step 1: Understand finalize() behavior

    finalize() is called by the garbage collector before object destruction, but timing is unpredictable.
  2. Step 2: Identify why relying on finalize() is discouraged

    Unpredictable timing can delay resource cleanup, causing problems like resource leaks.
  3. Final Answer:

    Because finalize() execution timing is unpredictable and may delay cleanup -> Option C
  4. Quick Check:

    Unpredictable finalize() timing discourages its use [OK]
Quick Trick: Finalize() timing is unpredictable; avoid relying on it [OK]
Common Mistakes:
  • Thinking finalize() runs immediately after creation
  • Believing finalize() frees memory instantly
  • Assuming finalize() must be called manually

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes