Java Garbage Collection Questions

JAVA

Java Garbage Collection Interview Questions

  1. What is Garbage Collection in Java?

  2. Why Java provides Garbage Collector?

  3. What is the purpose of gc() in Java?

  4. Explain the working of Garbage Collection in Java?

  5. When does an object become eligible for Garbage Collection in Java?

  6. Why do we use finalize() method in Java?

  7. Explain the different types of references in Java?

  8. How can we reference an unreferenced object again?

  9. What kind of process is the Garbage collector thread?

  10. Explain the purpose of the Runtime class?

  11. How can we invoke an external process in Java?

  12. What are the uses of Runtime class?

ANSWERS / HINTS

1- A mechanism to reclaim the memory of unused projects at run time. Garbage collection is also known as automatic memory management.

2- In Java, there are no pointers. Memory management and allocation are done by JVM. Since memory allocation is automated, after some time JVM may go low on memory. At that time, JVM has to free memory from unused objects. To help with the process of reclaiming memory, Java provides an automated process called Garbage Collector.

3- We have two methods System.gc() and Runtime.gc() to request the JVM to run the garbage collection. By using these methods, programmers can explicitly send a request for Garbage Collection. But the JVM process can reject this request and wait for some time before running the GC.

4- Java has an automated process called Garbage Collector for Memory Management. It is a daemon in JVM that monitors the memory usage and performs memory cleanup. Once JVM is low on memory, GC process finds the unused objects that are not referenced by other objects. These unused objects are cleaned up by Garbage Collector daemon in JVM.

5- An object can be Garbage Collected by JVM if it is not reachable.

There are two cases for deciding the eligibility of objects for Garbage Collection:

  • A set of circularly referenced instances that cannot be reached by any other instance outside that set.

  • An Object/instance that cannot be reached by a live thread.

6- Java provides finalize() method to perform any cleanup before Garbage Collection. This method is in the Object class, and it is invoked by JVM internally. Developers are free to implement this method for any custom cleanup in the case of Garbage Collection. If an Object is not Garbage Collected, then this method may not be called. This method is never invoked more than once by JVM.

7- There are four types of references:

  • Strong Reference

  • Soft Reference

  • Phantom Reference

  • Weak Reference

8- We can provide implementation in finalize() method to reference and unreferenced object. For an unreferenced object, finalize() method is called at the time of Garbage Collection. At this time, Object can pass its reference ‘this’ to finalize() method and revive itself.

9- Garbage Collection is a Daemon process in JVM. It is an internal process that keeps checking Memory usage and cleans up the memory.

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

11- Java provides the method Runtime.getRuntime().exec() to invoke an external process from JVM.

12- Runtime class in Java provides the following benefits:

  • It allows reading data via the keyboard

  • It helps in running non-java programs from within a java application.

  • It can use system properties and environment variables

 

Check Interview Questions on Java

Wikipedia Java

1 thought on “Java Garbage Collection Questions”

Comments are closed.