Core Java Interview Questions-Garbage Collection
This Core Java Interview Questions-Garbage Collection page provides all the core java interview questions on Garbage collection including what is garbage collection, the importance of finalize method, daemon thread and non-daemon threads, factory class, singleton class, runtime class and how to increase or decrease the size of JVM memory and Heap memory using predefined commands -Xms and -Xmx.
What is Garbage Collection?
Garbage collection is the mechanism of re acquiring the heap space by deleting the objects which are
eligible for garbage collection.
What is Garbage collector thread?
It is a daemon thread.
It destroys unused objects to and makes the heap space available to other objects in the application.
What is the use of finalize() method?
Available in java.lang.Object class used to place resource releasing logic of an Object.
Who will create and start Garbage Collector thread?
JVM creates and starts Garbage collector thread when required.
What is the process of Object destruction?
JVM start GC thread.
GC thread invokes finalize() method on un used object to release the resources connected to that
Destroy the heap space of an object.
Can we call Garbage collector thread directly?
No, but we can request the JVM to start GC thread in 2 ways
Who will start Garbage collector?
JVM starts GC thread when heap memory is running low.
When JVM starts GC thread?
When the heap memory is running low.
Why finalize() method is protected?
finalize() method contains destruction logic.
Destruction of resources mostly permitted in Is-A relation.
Only protected methods can be allowed to access in Parent-Child relation.
Who will stop Daemon threads forcefully and when?
JVM stops Daemon threads forcefully when all the non-daemon threads moved to the dead state.
What is Factory class?
Factory class cannot be instantiated directly.
We need to instantiate Factory class using the Factory method.
A factory method is a static method that returns Factory class object on request.
What is Singleton class?
Singleton class can be instantiated only once but the object can be shareable.
We cannot instantiate directly as it has the private constructor.
How to find JVM memory and Heap memory size?
Using pre defined methods of Runtime class.
maxMemory() returns JVM memory in bytes
totalMemory() returns Heap memory in bytes.
How increase JVM memory and Heap memory?
Increase JVM memory using -Xmx<size>m in megabytes
Heap memory using -Xms<size>m
What is Runtime Class in java?
Runtime class available in java.lang package.
Runtime class represents JVM.
How to instantiate Runtime class?
Using getRuntime() method.
It is the static method.
It returns Runtime class object.