hi...one interviewer asked me this Q..help me plzzz...
we can explicitly call the garbagecollector by 3 ways
1.object.finalise();
2.system.gc();
3.Runtime.getRuntime();
Among these,which one is the most effective one??
hi...one interviewer asked me this Q..help me plzzz...
we can explicitly call the garbagecollector by 3 ways
1.object.finalise();
2.system.gc();
3.Runtime.getRuntime();
Among these,which one is the most effective one??
system.gc(); is most effective . and Runtime.getRuntime(); will create runtime variable it will not execute garbage collector method.
system.gc();
System.gc()
system.gc();
system.gc();
System.gc()
However, you must know that you can not invoke garbage collector instantly.
Using System.gc() would place a request to JVM to garbage collect objects.
JVM would decide according to it's algorithm when to actually do so.
Further, it is not necessary that all objects eligible for garbage collection would be collected.
Regards,
Anand
j2eeconcepts dot com
which alogrithm used in Thread in java
1.object.finalise();
Finalizes the object (post mortem clean-up) which means it is ready to be garbage collected..
Finalization can also be achieved using - System.runFinalization();
the others 2 and 3 are the same.either of these are favoured over above
system.gc() is the answer suggested for u r question.....
Hey! that is a pretty smart question that got me thinking at first but then I checked
the jdk documentation of System, Object and Runtime classes. There i discovered this:
System.gc():-
public static void gc() {
Runtime.getRuntime().gc();
}
Thus The call System.gc() is effectively equivalent to the call Runtime.getRuntime().gc() which in turn is a native method:-
public native void gc();
As for the Object.finalize():-
protected void finalize() throws Throwable { }
The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition. So it definitely is not used to call the garbage collector explicitly rather we can override it so that when its called on our object after its been garbage collected, our desired cleanup can be performed.
So we come down to System.gc() and Runtime.getRuntime.gc() which r effectively the same. Furthermore calling either one does not guarantee garbage collection .
Regards,
Neeru