I encountered some troubles with WeakHashMap.
Consider this sample code:
List list = new ArrayList();
Map
The just-in-time compiler analyzes the code, sees that anObject
and anOtherObject
are not used after the loop, and removes them from the local variable table or sets them to null
, while the loop is still running. This is called OSR compilation.
Later the GC collects the strings because no strong references to them remain.
If you used anObject
after the loop you'd still get an OutOfMemoryError
.
Update: You'll find a more detailed discussion about OSR compilation in my blog.