Monday, 25 April 2016

How you can force the garbage collection ?


Garbage collection automatic process and can't be forced.

However, you could request it by calling System.gc()
JVM does not guarantee that GC will be started immediately.

Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program can't directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program.
Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists.

In Java, it is good idea to explicitly assign null into a variable when no more in use.
On calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.