上一篇文章讲的是对文件的加锁,但是在实际的过程中,会产生额外的内存消耗。一个重要的原因是,因为我们是把这些地址和锁存在一个HashMap中的,如果人为不清理的话,这个HashMap里面存放的内容就会越来越多。下面我讲另外一个Map的实现,WeakHashMap。
在介绍WeakHashMap之前,我先介绍一些WeakReference。
下面是java文档对weakreference的解释,http://docs.oracle.com/javase/6/docs/api/java/lang/ref/WeakReference.html
Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed. Weak references are most often used to implement canonicalizing mappings.
Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable. At that time it will atomically clear all weak references to that object and all weak references to any other weakly-reachable objects from which that object is reachable through a chain of strong and soft references. At the same time it will declare all of the formerly weakly-reachable objects to