Interview Question: Why Garbage Collection required?
What is garbage collection?
In two words
In two lines
“Managing the lifetime of object starting from creation, allocation and release it back to free memory pool.”
Why it is required?
We have two scenarios here,
Case 1: Where resources are not released.
Scenario: Resources will keep on getting consumed and will result in memory leak.
Case 2: Where resources are released irrespective of still being used in the code.
Scenario: May result in null pointers exceptions, corrupted memory areas or loss of data.
Garbage Collector manages the lifetime of all objects and avoids any early-release or late-release of resources.