ArrayList在进行add(),remove()操作时,对象都会被回收的

解决方案 »

  1.   

    我知道如果自己去写这么一个容器,如果没有在remove的时候把相应位置的对象释放,他将永远不会释放。不知道自带的容器会不会有这个问题!
      

  2.   

    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.
      

  3.   

    能说明WeakHashMap是怎样实象这种机制的吗