Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How garbage collector works in JVM in details?

user-image
Question added by shaik rafi , software developer , Innovative technologies
Date Posted: 2013/07/23

Java stores objects in heap.
Thus, if we keep on creating objects without clearing the heap, our computers might run out of heap space and we get ‘Out of Memory’ error.
Garbage Collection in Java is a mechanism which is controlled and executed by the Java Virtual Machine (JVM) to release the heap space occupied by the objects which are no more in use.
The JVM executes this process with the help of a demon thread called the ‘Garbage Collector’.
The garbage collector thread first invokes the finalize method of the object.
This performs the cleanup activity on the said object.
As a developer we cannot force the JVM to run the garbage collector thread.
Though there are methods e.g Runtime.gc () or System.gc(), but none of these assures the execution of garbage collector thread.
These methods are used to send garbage collection requests to the JVM.
It is up to the Java Virtual machine when it will initiate the garbage collection process.

Huzefa chikhly
by Huzefa chikhly , JAVA DEVELOPER , PRIMALINK SOLUTIONS INDIA PVT LTD

garbage collection is done by jvm in certain time interval(gap of time is uncertain). all the object in application are store in heap memory. there r some objects in heap which do not have any references this objects are knows as garbage as they r unneccesarily holding some memory in heap which is burden for application. so jvm free this memory from heap in some time interval which is uncertain this process is known as garbage collection. by writing Runtime.gc() we can explicitly tell jvm for garbage collection and jvm free all the unrefered object from heap memory. but is not necessary for programer to write code for garbage collection in java unlike c++. jvm autometicaly do all this work.

More Questions Like This