The Artima Developer Community
Sponsored Link

Java Buzz Forum
Top 15 Garbage Collection Interview Questions

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Top 15 Garbage Collection Interview Questions Posted: Mar 30, 2015 5:10 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Top 15 Garbage Collection Interview Questions
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

1.What is Garbage Collection in Java?

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

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;
    2.   
    3. class GarbageCollectionTest1{
    4.   
    5. public static void main(String [] args){
    6.  
    7. String str="garbage collection interview questions";
    8. // String object referenced by variable str and is not eligible for GC yet.
    9.  
    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;
    2.   
    3. class GarbageCollectionTest2{
    4.   
    5. public static void main(String [] args){
    6.  
    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.
    10.  
    11. str1=str2;
    12. //String object referenced by variable str1 is eligible for GC
    13.  
    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.

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

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

    Read: Top 15 Garbage Collection Interview Questions

    Topic: Is IntelliJ IDEA shining through Eclipse? Previous Topic   Next Topic Topic: Polymorphism in Java Generics

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use