StuffArrow is under maintenance. These are the demo deals and offers you are seeing here. We will come up with fresh and live deals and offers soon. Thanks for your patience. Keep visit our site for updates

Java Garbage Interview questions

What is Garbage Collection?

Garbage collection is a process of reclaiming the runtime unused objects. It is performed for memory management.


What is gc()?

gc() is a daemon thread.gc() method is defined in System class that is used to send request to JVM to perform garbage collection.


How JVM can destroy unreferenced object?

  • JVM internally uses a daemon thread called “garbage collector” to destroy all unreferenced objects.
  • A daemon thread is a service thread. Garbage Collector thread is called daemon thread because it provides services to JVM to destroy unreferenced objects.
  • This thread is low priority thread. Since it is a low priority thread we can not guarantee this execution.

What is the purpose of finalize() method?

finalize() method is invoked just before the object is garbage collected. It is used to perform cleanup processing.


Can an unreferenced objects be referenced again?



What kind of thread is the Garbage collector thread?

Daemon thread.


What is difference between final, finally and finalize?

final: final is a keyword, final can be variable, method or class. You, can’t change the value of final variable, can’t override final method, can’t inherit final class.

finally: finally block is used in exception handling. finally block is always executed.

finalize():finalize() method is used in garbage collection.finalize() method is invoked just before the object is garbage collected.The finalize() method can be used to perform any cleanup processing.


What is the purpose of the Runtime class?

The purpose of the Runtime class is to provide access to the Java runtime system.


How will you invoke any external process in Java?

By Runtime.getRuntime().exec(?) method.


So can you guarantee objects destruction?

  • No, we can not guarantee objects destruction even though it is unreferenced, because we can not guarantee garbage collector execution.
  • So, we can confirm whether object is eligible for garbage collection or not.


Can we force garbage collector?

  • No, we can not force garbage collector to destroy objects , but we can request it.


How can we request JVM to start garbage collection process?

  • We have a method called gc() in system class as static method and also in Runtime class as non static method to request JVM to start garbage collector execution.
  • System.gc();
  • Runtime.getRuntime().gc();


What is the algorithm JVM internally uses for destroying objects?

  • “mark and swap” is the algorithm JVM internally uses.


Which part of the memory is involved in Garbage Collection?

  • Heap.


What is responsibility of Garbage Collector?

  • Garbage Collector frees the memory occupied by the unreachable objects during the java program by deleting these unreachable objects.
  • It ensures that the available memory will be used efficiently, but does not guarantee that there will be sufficient memory for the program to run.


When does an object become eligible for garbage collection?

  • An object becomes eligible for garbage collection when no live thread can access it.


How many times does the garbage collector calls the finalize() method for an object?

  • Only once.


What happens if an uncaught exception is thrown from during the execution of finalize() method of  an object?

  • The exception will be ignored and the garbage collection (finalization) of that object terminates


What are the different ways to call garbage collector?

  • System.gc();
  • Runtime.getRuntime().gc();


How to enable /disable call of finalize() method of exit of application?

  • Runtime.getRuntime().runFinalizersOnExit(boolean value). passing the boolean value true and false will enable or disable the finalize() call.


%d bloggers like this: