The following snippet from RuntimeUtil.java from jlibs guarantees GC that garbage collection is done.
Since, it also uses System.gc(), i dont understand how can they guarantee that it will happen 100% of the times.
Following is the snippet:
/**
* This method guarantees that garbage collection is
* done unlike <code>{@link System#gc()}</code>
*/
public static void gc(){
Object obj = new Object();
WeakReference ref = new WeakReference<Object>(obj);
obj = null;
while(ref.get()!=null)
System.gc();
}
From what I can see it should work as they
1) create an object
2) get a “weak” reference to it
3) null the reference to it, in order to mark it for garbage collection
4) and wait with the while loop until it actually disappears