fork download
  1. import java.util.Map;
  2. import java.util.WeakHashMap;
  3.  
  4. class Test {
  5. public static void main(final String[] args) {
  6. final Map<A, B> map = new WeakHashMap<>();
  7. for (int i = 0; i < 10000; i++) {
  8. final B b = new B();
  9. final A a = b.a;
  10. map.put(a, b);
  11. }
  12.  
  13. for (int i = 0; i < 10; i++) {
  14. System.gc(); // Just to be sure :P
  15. }
  16.  
  17. System.out.println(map.size());
  18. }
  19.  
  20. private static class A {
  21. }
  22.  
  23. private static class B {
  24. public A a = new A();
  25. }
  26. }
Success #stdin #stdout 0.18s 320576KB
stdin
Standard input is empty
stdout
10000