Skip to content

Instantly share code, notes, and snippets.

@paulogaspar7
Forked from eribeiro/weakref-snippet.java
Created January 21, 2014 13:18
Show Gist options
  • Save paulogaspar7/8539858 to your computer and use it in GitHub Desktop.
Save paulogaspar7/8539858 to your computer and use it in GitHub Desktop.
WeakHashMap<Integer, String> map = new WeakHashMap<Integer, String>();
map.put(new Integer(10), "aaa"); // remove entry
// map.put(10, "aaa"); // don't remove entry
// map.put(Integer.valueOf(10), "aaa"); // don't remove entry
for (int i = 0; i < Integer.MAX_VALUE; i++) {
if (map.size() != 0) {
System.out.println("At iteration " + i + " the map still holds the reference to object");
} else {
System.out.println("Object has finally been garbage collected at iteration " + i + ", the map is now empty");
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment