Garbage collection identify the objects that are no longer referred to by any variable and then reclaims the memory occupied by the objects.
I don’t whether this process is done in a regular interval or as soon as an objects reference count falling down to zero.
suppose, if GC works immediately whenever an objects reference count falling down to zero then there is no need for requesting GC by calling System.GC();So, what is purpose of this method in this case?
When you call
System.gc(), you say to the garbage collector to make a clean-up. The problem is that it isn’t clear when theGCwill respond to your request. Even more, it is possible thatGCto not run at all when you call it. In java you cannot predict how theGCwill work. (That’s why is considered bad practice to put your cleanup code inObject‘sfinalize()method). InJava, the out of reference objects are collected for garbage automatically. That’s why you don’t need to callSystem.gc(). In special cases, when you want run it if possible, you can try to make use of this method, but the behavior is not guaranteed. (as specified above).