Garbage Collection Interview Questions in Java
Garbage Collection is one of the most important topics of Java. Garbage collection allows programs to perform automatic memory management. Garbage Collection interview questions are usually asked by interviewers.
1) Why Garbage Collection is necessary in Java?
In programming languages such as C and C++, the developer programmatically reclaims the space that is allocated to an object in the memory. In Java programming, the user is not responsible for managing the space memory used by the objects. In JVM, a garbage collection routine is added as its part, which is responsible for identifying and deleting objects that are no longer in use in memory.
2) What is the drawback to Garbage Collection?
The main drawback of Garbage Collection is that it freezes all those threads which are currently active at the time of occurring memory recovery phase. The garbage collection algorithms take time in seconds or minutes to run, and due to this, garbage collection routines can't be scheduled.
3) Explain the structure of Java Heap.
A chunk of memory shared among all threads is referred to as Java Heap. In Java, Heap is a combination of three generations such as new generation, old generation and PermGen space. To learn more about Heap structure, go through the following link:
4) Explain PermGen space in Java.
The internal representation of the Java classes held by JVM is included in the PermGen space. PermGen space contains garbage data collected in the same way as the heap's other parts (Young generation and Old generation) collected. To learn more about Heap structure, go through the following link:
5) Explain the difference between a minor, major and full garbage collection.
There is no official documentation or specification which can define the difference between major, minor and full garbage collection. We can define each one of them in the following way:
6) How will you identify major and minor garbage collections in Java?
We can identify the major and minor garbage collections based on the output. If the garbage collection logging is enabled by using -XX:PrintGCDetails or verbose:gc, the minor collection prints "GC", whereas the major collection prints "Full GC".
7) Differentiate ParNew and DefNew Young Generation garbage collectors.
ParNew and DefNew both are types of Young Generation garbage collectors. A multi-threaded garbage collector that is used with the concurrent Mark Sweep is referred to as a ParNew young generation garbage collector, whereas a single-threaded garbage collector that is used with the serial garbage collector is referred to as a DefNew young generation garbage collector.
8) Explain the use of the Finalize() method of the garbage collector.
The Finalize() method is called by the garbage collector before collecting any object that is eligible for the garbage collector. The Finalize() method is used to get the last chance to object to cleaning and free remaining resource.
9) Can we force the Garbage collector to run at any time?
No, we cannot force Garbage collection in Java. Although, we can request it by calling system.gc() or its cousin Runtime.getRunitime().get(). It's not guaranteed that GC will run immediately as a result of calling these methods.
10) Does Garbage collection occur in permanent generation space in JVM?
Yes, Garbage collection can occur in PermGen space. If PermGen cross a threshold or it is full, it can trigger Full Garbage Collector.
11) When does a Java object become available for garbage collection?
An object becomes available for garbage collection when:
12) What do you mean by mark-and-sweep?
Mark and Sweep are the two states of garbage collection. In the Mark stage, JVM identifies whether an object is still needed or not. The object is marked for garbage collection when the object is not needed.
In the Sweep stage, JVM performs memory reclamation and garbage collection algorithms.
13) Explain the difference between Minor, Major, and Full garbage collection.
There is no official documentation that can define the difference between Minor, Major, and Full garbage collection. However, it is commonly understood that:
14) What is a memory leak, and how does it affect garbage collection?
A situation where a garbage collector fails to identify and remove an unused object from memory is referred to as a memory leak.
The memory consumption is increased by the memory leak. After increasing the memory consumption, JVM is forced to clear more space for new objects. Mark and Sweep stages of garbage collection run more frequently. It free up less memory each time these stages run until there is no heap space left.
15) Can we trigger garbage collection from the code?
Yes, we can trigger the garbage collection by issuing a request to the JVM. We use the System.gc() command for issuing the request. However, there is no guarantee on when the JVM will respond to such a request.
16) Which part of the memory from Stack or Heap is involved in GC?
A heap part of the memory is involved in GC.
17) Explain the responsibilities of GC.
The main responsibility of garbage collection is to free up unused memory. GC ensures that the available memory will be used efficiently but does not guarantee that there will be enough memory to run the program.
18) What is a daemon thread? Is GC a daemon thread?
A thread which runs behind the application for performing background operations is referred to as a daemon thread.
Yes, GC is a daemon thread which starts by JVM.
19) Explain the ways for making an object eligible for GC when it is no longer needed?
There are the following three ways to make an object eligible for GC:
By setting all available object references to null
When the purpose of creating an object is achieved, we can set the reference of this object to null to make it available for GC.
By making reference variable for referring to another object
Decouple the reference variable from the object and set it refer to another object, so the object which it was referring to before reassigning is eligible for Garbage Collection.
By creating Islands of Isolation
When we have two instance variables referring to the instance of the same class, and these two variables also refer to each other and the object referred by these variables don't have any other valid reference, these two objects are eligible for GC.
20) What is the advantage of the Garbage collection?
The main advantage of the automatic garbage collection in Java is that it removes the burden of manual memory allocation and deallocation from us so that we can focus on problem solving.
21) What is the purpose of overriding finalize() method?
The finalize() method in Java also called finalizer is a method defined in java.lang.Object. It is called by Garbage collector just before collecting any object which is eligible for GC. Thus finalize() method provides last chance to object to do cleanup and free any remaining resource.
22) What are the different ways to make an object eligible for GC when it is no longer needed?
23) How many times does the garbage collector calls the finalize() method for an object?
24) is garbage collector a foreground or background thread?
Garbage collector is a background daemon thread. A daemon thread runs behind the application that is started by the JVM. The thread stops when all non-daemon foreground thread stops.
25) What is garbage collection time?
The more live objects are found, the longer the suspension, which has a direct impact on response time and throughput. This fundamental tenet of garbage collection and the resulting effect on application execution is called the garbage-collection pause or GC pause time.