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.
- 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.
- package com.instanceofjava;
- class GarbageCollectionTest1{
- public static void main(String [] args){
- String str="garbage collection interview questions";
- // String object referenced by variable str and is not eligible for GC yet.
- str=null;
- //String object referenced by variable str is eligible for GC
- }
- }
- 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
- package com.instanceofjava;
- class GarbageCollectionTest2{
- public static void main(String [] args){
- String str1="garbage collection interview questions";
- String str2="Top 15 garbage collection interview questions";
- // String object referenced by variable str1 and str2 and is not eligible for GC yet.
- str1=str2;
- //String object referenced by variable str1 is eligible for GC
- }
- }
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.
Java programming interview questions
- Print prime numbers?
- What happens if we place return statement in try catch blocks
- Write a java program to convert binary to decimal
- Java Program to convert Decimal to Binary
- Is it possible to print message without using System.out.println()
- Java program to restrict a class from creating not more than three objects
- Java basic interview programs on this keyword
- Java Program to Sort elements of Java ArrayList Example
- Interfaces allows constructors?
- Can we create static constructor in java
- Super keyword interview questions java
- Java interview questions on final keyword
- Can we create private constructor in java
- Java Program Find Second highest number in an integer array
- Java interview programming questions on interfaces
- Top 15 abstract class interview questions
- Java interview Questions on main() method
- Sort employee object by id in descending order using comparable and TreesSet
- Top 20 collection framework interview Questions
- Java Interview Program to find smallest and second smallest number in an array
- Java Coding Interview programming Questions : Java Test on HashMap
- Explain java data types with example programs
- How to check internet connection using java
- Constructor chaining in java with example programs
- Top 10 Interview Programs and questions on method overriding in java
- Swap two numbers without using third variable in java
- Find sum of digits in java
- How to create immutable class in java
- AtomicInteger in java
- Check Even or Odd without using modulus and division
- String Reverse Without using String API
- Find Biggest substring in between specified character
- Check string is palindrome or not?
- Reverse a number in java?
- Fibonacci series with Recursive?
- Fibonacci series without using Recursive?
- Sort the String using string API?
- Sort the String without using String API?
- what is the difference between method overloading and method overriding?
- How to find largest element in an array with index and value ?
- Sort integer array using bubble sort in java?
- Object Cloning in java example?
- Method Overriding in java?
- Program for create Singleton class?
- Print numbers in pyramid shape?
- Check armstrong number or not?
- Producer Consumer Problem?
- Remove duplicate elements from an array
- Convert Byte Array to String
- Print 1 to 10 without using loops
- Add 2 Matrices
- Multiply 2 Matrices
- How to Add elements to hash map and Display
- Sort ArrayList in descending order
- Sort Object Using Comparator
- Count Number of Occurrences of character in a String
- Can we Overload static methods in java
- Can we Override static methods in java
- Can we call super class static methods from sub class
- Explain return type in java
- Can we call Sub class methods using super class object?
- Can we Override private methods ?
- Basic Programming Questions to Practice : Test your Skill
- Java programming interview questions on collections
its really a good info for interview
ReplyDelete6.What is the algorithm JVM internally uses for destroying objects?
ReplyDeleteAnswer: Depends on the GC used and at which generation the object currently lives at.
12.How many times does the garbage collector calls the finalize() method for an object?
Answer: maybe once, maybe never
nice
DeleteGood article
DeleteHey Hii,
ReplyDeleteNice article..
Can you please solve my one problem.
In question 10, while running the program, There was the error shown ..."RUNTIME ERROR"..please resolve..
May be you are having problem with environment. ping in skype: instanceofjava
DeleteVery useful interview questions. Kindly share .net interview questions also.
ReplyDeleteit jobs in usa