public void testWeak() throws InterruptedException {
  Set<Object> objectSet=new WeakHashSet<Object>();
  objectSet.add(new Object());
  for (int i=0; i < 1000000; ++i) {
    new Object();
  }
  System.gc();
  Thread.sleep(100);
  assertTrue(objectSet.isEmpty());
}

public void testWeak ( ) throws InterruptedException { Set < Object > objectSet = new WeakHashSet < Object > ( ) ; objectSet . add ( new Object ( ) ) ; for ( int i = 0 ; i < 1000000 ; ++ i ) { new Object ( ) ; } System . gc ( ) ; Thread . sleep ( 100 ) ; assertTrue ( objectSet . isEmpty ( ) ) ; }
test that the references are weak . note that this test might prove to be flaky , since garbage collection cannot be guaranteed .
