Garbage collection in java interview questions – Garbage Collector Interview Questions in Java

Garbage collection in java interview questions: List of topic-wise frequently asked java interview questions with the best possible answers for job interviews.

Garbage Collector Interview Questions in Java

Garbage collection interview questions

Question 1.
What do you know about the garbage collector?
Answer: In Java, memory management is done automatically by JVM. A programmer is free of the responsibility of handling memory. A garbage collector is a part of JVM responsible for removing objects from the heap, which is no longer in use. The garbage collector typically runs in a background thread, periodically scanning the heap, identifying garbage objects, and releasing the memory they occupy so that the memory is available for future objects.

Question 2.
What is Garbage Collection and how to call it explicitly?
Answer:
When an object is no longer referred to by any variable, java automatically reclaims the memory used by that object. This is known as garbage collection. System.gc( ) method may be used to call it explicitly.

Question 3.
What is finalized ( ) method?
Answer:
finalize ( ) methods are used just before an object is destroyed and can be called just prior to garbage collection.

Question 4.
What is final, finalize( ), and finally?
Answer:
final: final keyword can be used for class, method, and variables.
A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods.
A final method can’t be overridden A final variable can’t change from its initialized value.
finalize( ): finalize( ) method is used just before an object is destroyed and can be called just prior to garbage collection.
finally: finally, a key word used in exception handling creates a block of code that will be executed after a try/catch block has been completed and before the code following the try/catch block. The final block will execute whether or not an exception is thrown.
For example, if a method opens a file upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. This final keyword is designed to address this contingency.

Question 5.
Explain the keywords – native, transient, volatile, finally.
Answer:
native: to use methods written in another language.
transient: if u declare the state of class as transient then it’s not saved in the persistent area.
volatile: this is not optimized at the time of optimization because its value may be changed implicitly or explicitly.
finally: finally is used to do work prior to closing the program because of exceptions. It’s always called by compiler either exception generated or not.

Question 6.
What is phantom memory?
Answer:
Phantom memory is a false memory. Memory that does not exist in reality.

Question 7.
What is garbage collection?
Answer:
If no reference to an object, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed. This is known as garbage collection.

Question 8.
Does garbage collection guarantee that a program will not run out of memory?
Answer:
Garbage collection does not guarantee that a program will not run out of memory. As garbage collection is JVM dependent then It is possible for programs to use memory resources faster than they are garbage collected. Moreover, garbage collection cannot be enforced, it is just suggested. Java guarantees that the finalize method will be run before an object is a garbage collected, it is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

The garbage collection is uncontrolled, which means you cannot predict when it will happen, you thus cannot predict exactly when the to finalize method will run. Once a variable is no longer referenced by anything it is available for garbage collection. You can suggest garbage collection with System.gc( ), but this does not guarantee when it will happen.

Question 9.
Can an object’s finalize( ) method be invoked while it is reachable?
Answer:
All objects finalize( ) method cannot be invoked by the garbage collector while the object is still reachable. However, an object’s finalized ( ) method may be invoked by other objects.

Question 10.
What are the disadvantages of reference counting in garbage collection?
Answer:
An advantage of this scheme is that it can run in small chunks of time closely interwoven with the execution of the program. This characteristic makes it particularly suitable for real-time environments where the program can’t be interrupted for very long. A disadvantage of reference counting is that it does not detect cycles. A cycle is two or more objects that refer to one another, for example, a parent object that has a reference to its child object, which has a reference back to its parent.

These objects will never have a reference count of zero even though they may be unreachable by the roots of the executing program. Another disadvantage is the overhead of incrementing and decrementing the reference count each time. Because of these disadvantages, reference counting currently is out of favor.

Question 11.
Is it advisable to depend on finalizing for all cleanups?
Answer:
The purpose of finalization is to give an opportunity to an unreachable object to perform any clean-up before the object is garbage collected, and it is advisable.

Question 12.
Describe the Garbage Collection process in Java?
Answer:
The JVM spec mandates automatic garbage collection outside of the programmer’s control. The System.gc( ) or Runtime.gc( ) is merely a suggestion to the JVM to run the GC process but is NOT guaranteed.

Question 13.
When is an object subject to garbage collection?
Answer:
An object is subject to garbage collection when it becomes unreachable to the program in which it is used.

Question 14.
What is the purpose of garbage collection?
Answer:
The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources may be reclaimed and reused.

Question 15.
What class of exceptions are generated by the Java run-time system?
Answer:
The Java runtime system generates RuntimeException and Error exceptions.

Question 16.
If an object is a garbage collected, can it become reachable again?
Answer:
Once an object is a garbage collected, it ceases to exist. It can no longer become reachable again.

Question 17.
How does garbage collection work?
Answer:
There are several basic strategies for garbage collection: reference counting, mark-sweep, mark-compact, and copying. In addition, some algorithms can do their job incrementally (the entire heap need not be collected at once, resulting in shorter collection pauses), and some can run while the user program runs (concurrent collectors). Others must perform an entire collection at once while the user program is suspended (so-called stop-the-world collectors). Finally, there are hybrid collectors, such as the generational collector employed by the 1.2 and later JDKs, that use different collection algorithms on different areas of the heap.

Question 18.
How can you free memory?
Answer:
With the help of finalize( ) method. If a programmer really wants to explicitly request a garbage collection at some point, System.gc( ) or Runtime.gc( ) can be invoked, which will fire off a garbage collection at that time.

Question 19.
What is the purpose of finalization?
Answer:
The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.

Question 20.
Explain garbage collection?
Or
How you can force the garbage collection?
Or
What is the purpose of garbage collection in Java, and when is it used?
Or
What is Garbage Collection and how to call it explicitly?
Or
Explain the Garbage collection mechanism in Java?
Answer:
Garbage collection is one of the most important features of Java. The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory.

Every class inherits finalize( )  method from java. lang. Object, the finalize( ) method is called by the garbage collector when it determines no more references to the object exists. In Java, it is a good idea to explicitly assign null into a variable when no more in use. In Java on calling System.gc( ) and Runtime.gc( ), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will be garbage collected. Garbage collection is an automatic process and can’t be forced. There is no guarantee that Garbage collection will start immediately upon the request of System.gc( ).

Question 21.
What kind of thread is the Garbage collector thread?
Answer:
It is a daemon thread.

Question 22.
What is gc( )?
Answer:
gcO is a daemon thread.gc( ) method is defined in System class that is used to send requests to JVM to perform garbage collection.

Question 23.
Can an unreferenced objects be referenced again?
Answer:
Yes.

Question 24.
How will you invoke any external process in Java?
Answer:
By Runtime.getRuntime().exec(?) method.

Question 25.
Does Java have destructors?
Answer:
The garbage collector does the job working in the background. Java does not have destructors, but it has finalizers that do a similar job.
The syntax is:
public void finalize( ){
}
if an object has a finalizer, the method is invoked before the system garbage collects the object.

Question 26.
What is the difference between Serial and Throughput Garbage collectors?
Answer:
The throughput garbage collector uses a parallel version of the young generation collector and is meant to be used with applications that have medium to large data sets. On the other hand, the serial collector is usually adequate for most small applications (those requiring heaps of up to approximately 100MB on modern processors).