Java tutorial
//package com.java2s; import java.lang.ref.WeakReference; public class Main { public static void forceGCandWait() { Object obj = new Object(); WeakReference ref = new WeakReference<>(obj); obj = null; System.gc(); System.runFinalization(); /** wait for garbage collector finished*/ while (ref.get() != null) try { Thread.sleep(100); } catch (InterruptedException e) { } //System.gc(); } }