Listing A: ReferenceQueue example
import java.lang.ref.*;
import java.util.*;

public class MemoryTest3 {
  public static void main( String[] args ) {
   ReferenceQueue queue = new ReferenceQueue();
   ArrayList blocks = new ArrayList();
   int size = 65536;
    for ( int id=0; true; id++ ) {
     blocks.add( new MyReference(new MemoryBlock(id,size),queue) );
      while ( true ) {
        Reference ref = queue.poll();
        if ( ref == null )
          break;
       blocks.remove( ref );
      }
     System.out.println( "blocks: "+blocks );
      size *= 2;
    }
  }
}