public static void forceGC(){
  WeakReference<Object> dumbReference=new WeakReference<>(new Object());
  while (dumbReference.get() != null) {
    System.gc();
    try {
      Thread.sleep(500);
    }
 catch (    InterruptedException e) {
    }
  }
}

public static void forceGC ( ) { WeakReference < Object > dumbReference = new WeakReference < > ( new Object ( ) ) ; while ( dumbReference . get ( ) != null ) { System . gc ( ) ; try { Thread . sleep ( 500 ) ; } catch ( InterruptedException e ) { } } }
some testcases are time sensitive , and we need to make sure a gc would happen before certain scenarios
