What is garbage collection in Java?
Garbage collection is an automatic memory management process that removes unused objects and frees memory at runtime.
Why is garbage collection needed?
To prevent memory leaks and reclaim memory occupied by objects no longer in use.
Who performs garbage collection?
The JVM’s Garbage Collector.
What kind of memory does garbage collection manage?
Heap memory.
When does an object become eligible for garbage collection?
When no reachable references point to it.
Is garbage collection deterministic?
No. JVM decides when to run it.
Can you force garbage collection manually?
You can request it using System.gc(), but execution is not guaranteed.
Does garbage collection delete variables or objects?
It reclaims memory of unreachable objects.
What is automatic memory management?
A system where the runtime handles allocation and deallocation instead of the programmer.
What happens before an object is garbage collected?
Its finalize() method may run (deprecated feature).
What is the main benefit of garbage collection?
It reduces risk of memory errors like dangling pointers.
Does garbage collection improve program safety?
Yes. It prevents manual memory handling mistakes.
Can an object resurrect itself during GC?
Previously possible using finalize(), but this practice is discouraged.
Does garbage collection eliminate all memory issues?
No. Logical memory leaks can still occur if references remain.
Which memory area is not garbage collected?
Stack memory.
Key rule to remember?
Garbage collection = automatic cleanup of unused heap objects.
Why does Java provide a garbage collector?
To automatically reclaim memory from unused objects and prevent manual memory management errors.
Why is automatic memory management important in Java?
It removes the need for programmers to manually allocate and free memory.
What problem would occur without garbage collection?
Applications could run out of memory due to unused objects remaining allocated.
Who manages memory allocation in Java?
The JVM.
When does the garbage collector run?
When the JVM determines memory needs to be reclaimed.
What advantage does GC provide over manual memory management?
It prevents common bugs like dangling pointers and memory leaks.
Does Java use pointers like C/C++?
No. Java uses references instead of direct memory pointers.
Why does JVM need to reclaim memory automatically?
Because programs continuously create objects during execution.