dongjb(光速) ,你错了。容器对对象的引用也是引用。 所以没有remove的时候,就算这个对象在没有别的引用,他也不会被回收。 为了解决这个问题,jdk提供了这样一个容器:java.util.WeakHashMap在这个容器内的对象,如果除了容器本身的饮用,再也没有别的饮用指向容器内的对象,这个对象就会被回收。文档说明: A hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently than other Map implementations.
所以没有remove的时候,就算这个对象在没有别的引用,他也不会被回收。
为了解决这个问题,jdk提供了这样一个容器:java.util.WeakHashMap在这个容器内的对象,如果除了容器本身的饮用,再也没有别的饮用指向容器内的对象,这个对象就会被回收。文档说明:
A hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently than other Map implementations.