Java Garbage Collection 1740227202
Java Garbage Collection 1740227202
garbage collection
interview questions
What is garbage collection in Java?
Answer:
Garbage Collection is an automated process in Java that reclaims
memory occupied by objects no longer referenced, ensuring
efficient memory management and preventing memory leaks.
What happens if the heap is full and garbage collection cannot free up space?
Answer:
If the heap is full and GC cannot reclaim enough memory, the JVM
throws an OutOfMemoryError.
How do you prevent memory leaks in Java?
Answer:
Use weak references for cache-like structures.
Remove listeners, callbacks, or references when objects are no
longer needed.
Avoid static references to objects.
Use tools like Eclipse MAT to detect memory leaks.
What are the different regions of the heap?
Answer:
Young Generation:
Divided into Eden and Survivor Spaces.
Objects are created in Eden and moved to Survivor spaces if they survive
garbage collection.
Old Generation (Tenured):
Stores long-lived objects that survived multiple GC cycles in the Young
Generation.
Metaspace:
Stores class metadata and is separate from the heap.
What is stop-the-world (STW) in garbage collection?
Answer:
STW refers to the JVM pausing all application threads during certain
phases of garbage collection, which can impact application
performance.