FQA ON GARBAGE COLLECTION IN JAVA
INTERVIEW QUESTIONS ON GARBAGE COLLECTION IN
JAVA
- What is
structure of Java Heap
When a Java program started
Java Virtual Machine gets some memory from Operating System. Java Virtual
Machine or JVM uses this memory for all its need and part of this memory is
call java heap memory. Heap in Java generally located at bottom of address space
and move upwards. whenever we create an object using new operator or by any
another means the object is allocated memory from Heap and When object dies or
garbage collected, memory goes back to Heap space in Java.
- When does an Object becomes
eligible for Garbage collection in Java ?
Answer : An object becomes eligible for garbage collection when
there is no live reference for that object or it can not be reached by any live
thread. Cyclic reference doesn’t count as live reference and if two objects are
pointing to each other and there is no live reference for any of them, than
both are eligible for GC. Also Garbage collection thread is a daemon threadwhich will run by JVM based upon
GC algorithm and when runs it collects all objects which are eligible for GC.
- What is finalize method in Java ? When does
Garbage collector calls finalize method in Java ?
Answer : Finalize
method in Java also called finalizer is a method defined in java.lang.Object and called by
Garbage collector before collecting any object which is eligible for GC. Finalize() method
provides last chance to object to do cleanup and free any remaining resource,
to learn more about finalizers, read What is finalize method in Java.
- If Object A has reference to Object B and
Object B refer to Object A, apart from that there is no live reference to
either object A or B, Does they are eligible to Garbage collection ?
This Garbage
collection interview questions is related question 5 “When object become
eligible for Garbage collection”. An object becomes eligible for Garbage
collection if there is no live reference for it. It can not be accessible from
any Thread and cyclic dependency doesn’t prevent Object from being Garbage
collected. Which means in this case both Object A and Object B are eligible of
Garbage collection.
- Can
we force Garbage collector to run at any time ?
Answer : No, you
can not force Garbage collection in Java. Though you can request it by
calling Sytem.gc() or its cousin Runtime.getRuntime().gc(). It’s not
guaranteed that GC will run immediately as result of calling these method.
6. Which part of the memory is involved in
Garbage Collection? Stack or Heap?
Ans) Heap
7. What is responsiblity of Garbage
Collector?
Ans) 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.
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.
8. Is garbage collector a daemon
thread?
Ans) Yes
GC is a daemon thread. A daemon thread runs behind the application. It is
started by JVM. The thread stops when all non-daemon threads stop.
9. How is Garbage Collection
managed?
Ans)The
JVM controls the Garbage Collector; it decides when to run the Garbage
Collector. JVM runs the Garbage Collector when it realizes that the memory is
running low. The behavior of GC can be tuned by passing parameters to JVM. One
can request the Garbage Collection to happen from within the java program but
there is no guarantee that this request will be taken care of by jvm.
-
When does an object become eligible
for garbage collection?
Ans) An object becomes eligible for Garbage
Collection when no live thread can access it.
11. What is the purpose of overriding finalize()
method?
Ans) 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?
Ans) Only once.
13. What happens if an uncaught
exception is thrown from during the execution of the finalize() method of an
object?
Ans) The exception will be
ignored and the garbage collection (finalization) of that object terminates.
14. What are different ways to call garbage
collector?
Ans) Garbage collection can be
invoked using System.gc()
or Runtime.getRuntime().gc().
15. How to enable/disable call of finalize()
method of exit of the application
Ans) Runtime.getRuntime().runFinalizersOnExit(boolean
value) . Passing the boolean value will either disable or
enable the finalize() call.
16. What is the purpose of garbage
collection in Java, and when is it used ?
The purpose of garbage collection is to identify and discard
those objects that are no longer needed by the application, in order for the
resources to be reclaimed and reused.
17. What does System.gc() and
Runtime.gc() methods do ?
These methods can be used as a hint to the JVM, in order to
start a garbage collection. However, this it is up to the Java Virtual Machine
(JVM) to start the garbage collection immediately or later in time.
18.When is the finalize() called ? What is the purpose of
finalization ?
The finalize method is called by the garbage collector, just
before releasing the object’s memory. It is normally advised to release
resources held by the object inside the finalize method.
19. If an object reference is set to
null, will the Garbage Collector immediately free the memory held by that
object ?
No, the object will be available for garbage collection in the
next cycle of the garbage collector.
20. What
Is The Algorithm Jvm Internally Uses For Destroying Objects?
"mark and swap" is the algorithm JVM
internally uses.
Comments
Post a Comment