Top Java Garbage Collection Interview Questions (2024)
What is Java Garbage Collection?
Is there PermGen space in the Java8 onwards version?
What is Metaspace?
What tools are available to monitor Metaspace?
What is the Dangling pointer bug in Garbage Collection?
What is Double free bugs in Garbage Collection?
What is minor or incremental garbage collection?
What does it mean by major or full garbage collection?
What is "stop-the-world" in Garbage Collection?
How did Java tried to resolve the Stop-the-World problem in Garbage Collection?
Which are four types of garbage collectors?
What are the most important Garbage Collection metrics to track?
How do I fine-tune garbage collection?
What is Java Heap Space?
Explain in detail how garbage collection works?
Q: What is Java Garbage Collection?
Ans:
Garbage collection (GC) in the Java virtual machine (JVM) is responsible for automatically determining what memory is no longer in use by a Java application and recycling it for other purposes.
Q: Is there PermGen space in the Java8 onwards version?
Ans:
In JDK 8.0, Permanent Generation (PermGen) memory space has been entirely removed.
If the PermSize and MaxPermSize JVM arguments are available at startup, they have been neglected
and a warning is issued.
Q: What is Metaspace?
Ans:
After Java removed PermGen, a new memory space Metaspace was introduced in Java 8.
The JDK 8 HotSpot JVM, like the Oracle JRockit and IBM JVMs, now uses native memory for the representation of class metadata, which is referred to as Metaspace.
Now, you will not encounter or get java.lang.OutOfMemoryError
PermGen exceptions
happening due to the limited size of the PermGen space of the heap.
When the class metadata usage exceeds the MaxMetaspaceSize limit, metaspace garbage collection is triggered. Extreme Metaspace garbage collection could be a sign of a class, classloader memory leak, or insufficient sizing for our application.
Take a look at our Suggested Posts :
Q: What tools are available to monitor Metaspace?
Ans:
The HotSpot 1.8 verbose GC log output shows metaspace usage.
Note: Older tools having PermGen space references, like b75, Jstat and JVisualVM have
not been updated, so we can't go with these tools for monitoring metspace.
Q: What is the Dangling pointer bug in Garbage Collection?
Ans:
Dangling pointer bugs happen whenever a piece of memory is freed (during object destruction) but there are still pointers to it, and one of those pointers is dereferenced. By then, the memory could have been reassigned to some other use, with unpredictable results.
Q: What is Double free bugs in Garbage Collection?
Ans:
Double free bugs occur when a program attempts to free a region of memory that has already been freed and possibly allocated again.
Q: What is minor or incremental garbage collection?
Ans:
Once unreachable objects in young generation heap memory are removed, it is said that a minor or incremental garbage collection has occurred.
Q: What does it mean by major or full garbage collection?
Ans:
When the objects which survived the minor garbage collection and were copied into the old generation or permanent generation heap memory are removed, a major or full garbage collection has occurred. Garbage collection occurs less frequently in the old generation than in the younger generations.
Q: What is "stop-the-world" in Garbage Collection?
Ans:
The JVM will stop the application for at least a short time and execute GC to free up memory. The procedure is known as "stop-the-world". This means that all threads will stop running except the GC threads until the GC threads have completed and the garbage collector has freed up all objects.
Q: How did Java tried to resolve the Stop-the-World problem in Garbage Collection?
Ans:
Java 11 has some great features, one is Z Garbage Collector (ZGC). The Z Garbage Collector, also known as ZGC, is a low latency scalable garbage collector designed to meet the following objectives.
- Pause times shall not exceed 10 ms
- Handle heaps ranging from a few hundred megabytes to multi terabytes in size
- Pause times do not increase with the size of the heap or live-set.
Modern JVMs, such as Azul Zing, use the Continuously Concurrent Compacting Collector (C4), that eliminates the stop-the-world GC pauses which limit scalability in traditional JVMs.
Q: Which are four types of garbage collectors?
Ans:
At its most simple, Garbage Collection includes finding memory that is no longer in use and making it available for reuse.
Java has below four types of garbage collectors:
Serial Garbage Collector
with only one thread executing the GCParallel Garbage Collector
Simultaneous execution of multiple minor threads, each of which executes a part of the GCCMS Garbage Collector
CMS, which is equivalent to parallel, means allowing the execution of some application threads while also reducing the frequency of stop-the-world GC.G1 Garbage Collector
G1, that is also run in parallel and concurrently but it does have a different function than CMS
Refer
Z Garbage Collector (ZGC) to understand in detail.
Q: Why is it necessary and important to monitor garbage collection?
Ans:
Garbage collection could have an unpredictable impact on the performance of a Java application. Whenever there is a lot of GC activity, it puts a lot of burden on the CPU and slows down application processing. As a result, business transactions are executed slowly, affecting the user experience of end users accessing the Java application.
Q: What are the most important Garbage Collection metrics to track?
Ans:
Below are few important points to keep track in Garbage Collection metrics:
- When the garbage collection took place
- How frequently is garbage collected?
- How much memory is gathered each time?
- How long does garbage collection running?
- JVM garbage collection time as a percentage of total time
- The kind of garbage collection took place - minor or full GC?
- JVM heap and non-heap memory utilisation
- JVM CPU utilisation
Q: How do I fine-tune garbage collection?
Ans:
Here are two common approaches:
- Reduce the number of objects passed to the old generation area as much as possible.
- Set the major (or full) GC time to be short.
-Xms, -Xmx, and -NewRatio are a few crucial JVM parameters to configure for right-sizing the JVM's memory (ratio of new generation and old generation size)
Q: What is Java Heap Space?
Ans:
The Java runtime allocates memory to Objects and JRE classes using heap space. Whenever we make an object, it always makes it in the Heap space.
Garbage Collection runs on heap memory to free memory used by objects that do not have a reference. Any object created in heap space has global access and can be accessed from anywhere in the application.
Q: What is Java Stack Memory?
Ans:
Java Stack memory is used to execute a thread. They contain method-specific, short-lived values as well as references to other objects in the heap that are referred to by the method.
Stack memory is always accessed in the last-in-first-out (LIFO) order.Whenever a method is called, a new block in stack memory is created for the method to keep local primitive values and references to other objects in the method.
Q: Explain in detail how garbage collection works?
Ans:
The heap is subdivided into smaller spaces, known as generations. These areas are known as the Young Generation, the Old or Tenured Generation, and the Permanent Generation.
Young Generation
The Young Generation is where new objects are created.
The Young Generation is further subdivided into the following groups:
Eden space
All new objects begin in Eden space, and initial memory is assigned to them.Survivor spaces (Survivor 1 and Survivor 2)
after surviving one garbage collection cycle, objects are moved here from Eden.
It's said to be a minor garbage collection event, when objects are garbage collected from the Young Generation,
A Minor GC is performed when Eden space is completely filled with objects. All dead objects are removed, and all living objects are relocated to one of the survivor spaces. Minor GC also examines the objects in one survivor space and continues to move them to the other.
Old Generation
Long-lived objects are moved from the Young Generation to the Old Generation. This is also called the Tenured Generation, and it also has objects that have been in survivor spaces for a long period of time.
It's said to be a major garbage collection event, when objects are garbage collected from the Old Generation.
- When a new object is created, it is first placed in the young generation's Eden space.
- When minor garbage collection occur, the live objects from Eden are moved to the Survivor 1 Space .
- When the next minor garbage collection occurs, the live objects from both Eden and Survivor 1 Space are moved to the Survivor 2 Space.
- This cycle is repeated for a specific number of times. If the object is still in use after this step, it will be moved to the old generation space in the next garbage collection cycle.
Permanent Generation (before Java 8 Version)
The Permanent Generation stores metadata such as classes and methods. The JVM populates it at runtime based on the classes in use by the application. Classes which are no longer being used may be collected as garbage by the Permanent Generation.MetaSpace (since from Java 8)
The PermGen memory space has been replaced by the MetaSpace memory space since about Java 8. The implementation differs from PermGen in that this heap space is now automatically resized.
The JDK 8 HotSpot JVM, like the Oracle JRockit and IBM JVMs, now uses native memory for the representation of class metadata, which is referred to as Metaspace.
Now, you will not encounter or get java.lang.OutOfMemoryError: PermGen exceptions happening due to the limited size of the PermGen space of the heap.