» » Top 15 Garbage Collection Interview Questions

1.What is Garbage Collection in Java?

  • Garbage Collection is an automatic memory management feature.
  • The process of destroying unreferenced objects is called Garbage Collection.
  • Once object is unreferenced it is considered as unused object, hence JVM automatically destroys that object.
  • In java developers responsibility is only to creating objects and unreferencing those objects after usage.

2.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.

 3.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.

4.Can we force garbage collector?

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

5.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();

6.What is the algorithm JVM internally uses for destroying objects?

  • "mark and swap" is the algorithm JVM internally uses.

7.Which part of the memory is involved in Garbage Collection?

  • Heap.

8.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.

9. When does an object become eligible for garbage collection?

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

10. What are the different ways to make an object eligible for garbage collection when it is no longer needed?

  • Set all available object references to "null" once the purpose of creating object is served.

  1. package com.instanceofjava;
  3. class GarbageCollectionTest1{
  7. String str="garbage collection interview questions";
  8. // String object referenced by variable str and is not eligible for GC yet.
  10. str=null;
  11. //String object referenced by variable str is eligible for GC
  12. }
  13. }

  • Make the reference variable to refer to another object. Decouple the reference variable from the object and set it refer to another object, so the object which was referring to before reassigning is eligible for Garbage Collection

  1. package com.instanceofjava;
  3. class GarbageCollectionTest2{
  7. String str1="garbage collection interview questions";
  8. String str2="Top 15 garbage collection interview questions";
  9. // String object referenced by variable str1 and str2 and is not eligible for GC yet.
  11. str1=str2;
  12. //String object referenced by variable str1 is eligible for GC
  14. }
  15. }

11.What is purpose of overriding finalize() method?

  • The finalize() method should be overridden for an object to include the clean up code or to dispose of the system resources that should to be done before the object is garbage collected.


  • Only once.

13.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

14.What are the different ways to call garbage collector?

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

15. 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.

Java programming interview questions
  1. Print prime numbers? 
  2. What happens if we place return statement in try catch blocks 
  3. Write a java program to convert binary to decimal 
  4. Java Program to convert Decimal to Binary
  5. Java program to restrict a class from creating not more than three objects
  6. Java basic interview programs on this keyword 
  7. Interfaces allows constructors? 
  8. Can we create static constructor in java 
  9. Super keyword interview questions java 
  10. Java interview questions on final keyword
  11. Can we create private constructor in java
  12. Java Program Find Second highest number in an integer array 
  13. Java interview programming questions on interfaces 
  14. Top 15 abstract class interview questions  
  15. Java interview Questions on main() method  
  16. Top 20 collection framework interview Questions
  17. Java Interview Program to find smallest and second smallest number in an array 
  18. Java Coding Interview programming Questions : Java Test on HashMap  
  19. Explain java data types with example programs 
  20. Constructor chaining in java with example programs 
  21. Swap two numbers without using third variable in java 
  22. Find sum of digits in java 
  23. How to create immutable class in java 
  24. AtomicInteger in java 
  25. Check Even or Odd without using modulus and division  
  26. String Reverse Without using String API 
  27. Find Biggest substring in between specified character
  28. Check string is palindrome or not?
  29. Reverse a number in java?
  30. Fibonacci series with Recursive?
  31. Fibonacci series without using Recursive?
  32. Sort the String using string API?
  33. Sort the String without using String API?
  34. what is the difference between method overloading and method overriding?
  35. How to find largest element in an array with index and value ?
  36. Sort integer array using bubble sort in java?
  37. Object Cloning in java example?
  38. Method Overriding in java?
  39. Program for create Singleton class?
  40. Print numbers in pyramid shape?
  41. Check armstrong number or not?
  42. Producer Consumer Problem?
  43. Remove duplicate elements from an array
  44. Convert Byte Array to String
  45. Print 1 to 10 without using loops
  46. Add 2 Matrices
  47. Multiply 2 Matrices
  48. How to Add elements to hash map and Display
  49. Sort ArrayList in descending order
  50. Sort Object Using Comparator
  51. Count Number of Occurrences of character in a String
  52. Can we Overload static methods in java
  53. Can we Override static methods in java 
  54. Can we call super class static methods from sub class 
  55. Explain return type in java 
  56. Can we call Sub class methods using super class object? 
  57. Can we Override private methods ? 
  58. Basic Programming Questions to Practice : Test your Skill
  59. Java programming interview questions on collections

    Instance Of Java

    Newer Post
    Older Post

    7 comments for Top 15 Garbage Collection Interview Questions

    1. its really a good info for interview

    2. 6.What is the algorithm JVM internally uses for destroying objects?
      Answer: Depends on the GC used and at which generation the object currently lives at.

      Answer: maybe once, maybe never

    3. Hey Hii,
      Nice article..
      Can you please solve my one problem.
      In question 10, while running the program, There was the error shown ..."RUNTIME ERROR"..please resolve..

      1. May be you are having problem with environment. ping in skype: instanceofjava

    4. Very useful interview questions. Kindly share .net interview questions also.
      it jobs in usa


    Select Menu