Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Top 25+ Java Garbage Collection Interview Questions

Here, we have incorporated the most important 25+ Garbage Collection interview questions in java with the best possible answers.

These Garbage Collection interview questions can be asked from freshers or 1 to 3 years of experience in any java interviews.

If you prepare answers for these kinds of questions, you can definitely answer all questions asked by the interviewer. So, let’s start preparation.

Garbage Collection Interview Questions in Java


1. What is Garbage Collection in Java?

Ans: Garbage collection is an automatic process of freeing up heap memory allocated to objects that are no longer in use.

2. What is Garbage Collector?

Ans: The program that performs the Garbage collection process is called garbage collector.

3. In which part of memory, objects and object references are created?

Ans: All Java objects are always created on the heap memory whereas, object references are created in stack memory.

4. Which algorithm is used by JVM internally for garbage collection?

Ans: JVM internally uses the mark and swap algorithm for garbage collection.

5. What is the main purpose of garbage collection in Java?

Ans: The main objective/purpose of garbage collection in java is to identify and delete objects that are no longer needed by a program so that their resources may be reclaimed and reused.

Garbage collection provides an automated memory management feature that is controlled by the Java runtime system (JVM) during runtime. JVM removes all the objects that are no longer in use and free up memory.


6. Which method can be used to send request to JVM for performing garbage collection?

Ans: System class provides gc() method that can be used for requesting JVM to run garbage collection.

7. Can an unreferenced object be referenced again?

Ans: Yes, an unreferenced object can be referenced again.

8. Which type of thread is garbage collector thread?

Ans: Garbage collector thread runs a low priority daemon thread for clearing memory.

9. When an object is eligible for garbage collection?

Ans: An object is eligible for garbage collection when

  • we explicitly assign null to the reference variable.
  • we assign one reference variable to another reference variable.
  • the reference variable is out of scope.

10. When does the Garbage Collection happen?
Or, when does garbage collection gets executed in java?

Ans: JVM handles garbage collection and decides when to execute garbage collection. It is also possible to call garbage collection through the java program.

But there is no guarantee that JVM will definitely execute garbage collection when the program will be run. Generally, JVM triggers garbage collection only if there is not enough memory or when the daemon GC thread (low priority) gets a chance to run.

11. How does garbage collector work in java?

Ans: Java Virtual Machine (JVM) deletes objects when it decides that they are no longer being used. This process is known as garbage collection.

Java Garbage collector periodically frees the memory used by objects that are no longer needed. The garbage collector uses a mark and swap algorithm that scans dynamic memory areas for objects and marks those that are referenced.

After examing all possible referenced objects, those objects that are not marked (i.e. are not referenced) are said to be garbage and are collected.


12. Can an object be garbage collected while it is still reachable in the program?

Ans: A reachable object cannot be garbage collected. Only unreachable objects may be garbage collected.

13. If an object gets garbage collected, can it become reachable again?

Ans: Once an object gets garbage collected, it ceases to exist. It can no longer become reachable again.

14. What are the different ways to invoke garbage collector (GC)?

Ans: There are two ways to send request to JVM to run garbage collector. They are:

  • Using Runtime.getRuntime.gc() method
  • Using System.gc() method

15. What is the function of gc() method in Java?

Ans: The function of gc() method is to send request to JVM to run garbage collection. By using this method, we can explicitly send request for Garbage Collection. But JVM can ignore this request and wait for some time before running the GC.


16. What is finalization in Java?

Ans: Finalization in Java is an action that is automatically performed on an object before the memory occupied by the object is freed up by the garbage collector.

17. What is finalizer in Java?

Ans: The block of code that contains the action to be performed is called finalizer in java.

18. What is the purpose of finalization?

Ans: The purpose of finalization is to give an opportunity to an unreachable object before an object gets garbage collection.

19. What is a finalize() method? When is it called?

Ans: finalize() method is an instance method that is provided by Object class. Inside the finalize( ) method, we write the finalization code for those actions that must be performed before an object is destroyed by the garbage collector.

finalize() method is invoked by the garbage collector just before releasing the object’s memory.


20. How many times may finalize() method of Object class be called by the garbage collector for an object?

Ans: A finalize() method of Object class may only be called once by the garbage collector.

21. Can the finalize() method be called while it is reachable?

Ans: An object’s finalize() method cannot be called by GC while the object is still reachable. However, an object’s finalize() method may be called by other objects.

22. Under what conditions is finalize() method called by the garbage collector?

Ans: The garbage collector calls the finalize() method when it detects that the object has become unreachable.

23. What would happen if an uncaught exception is thrown during the execution of the finalize() method on an object?

Ans: The exception will be ignored and the finalization of that object ends.

24. Can the Garbage Collection be forced by any means?

Ans: No, the Garbage Collection can not be forced by any means. However, there are a few ways by which we can send request to JVM to run garbage collection but there is no guarantee that these requests will be taken care of by JVM.

25. Can finalize() method be called on all Java objects?

Ans: Yes because all Java classes extend the Object class, therefore, the finalize() method can be called on all Java objects.

26. Finally or Finalize which is better to use?

Ans: Refer to this tutorial: Object finalization in Java


Hope that this tutorial has incorporated all types of garbage collection interview questions in java that can be asked in any java interview. I hope that you will have understood the answers to ​these garbage collections questions and prepared for interviews.

All the best!!!

The post Top 25+ Java Garbage Collection Interview Questions appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Top 25+ Java Garbage Collection Interview Questions

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×