引用链接:http://www.ibm.com/developerworks/cn/java/j-jtp11225/
虽然用 Java™ 语言编写的程序在理论上是不会出现“内存泄漏”的,但是有时对象在不再作为程序的逻辑状态的一部分之后仍然不被垃圾收集。
要让垃圾收集(GC)回收程序不再使用的对象,对象的逻辑 生命周期(应用程序使用它的时间)和对该对象拥有的引用的实际 生命周期必须是相同的。在大多数时候,好的软件工程技术保证这是自动实现的,不用我们对对象生命周期问题花费过多心思。但是偶尔我们会创建一个引用,它在内存中包含对象的时间比我们预期的要长得多,这种情况称为无意识的对象保留(unintentional object retention)。
全局 Map 造成的内存泄漏
无意识对象保留最常见的原因是使用 Map
将元数据与临时对象(transient object)相关联。假定一个对象具有中等生命周期,比分配它的那个方法调用的生命周期长,但是比应用程序的生命周期短,如客户机的套接字连接。需要将一些元数据与这个套接字关联,如生成连接的用户的标识。在创建 Socket
时是不知道这些信息的,并且不能将数据添加到 Socket
对象上,因为不能控制 Socket
类或者它的子类。这时,典型的方法就是在一个全局 Map
中存储这些信息,如清单 1 中的 SocketManager
类所示:
清单 1. 使用一个全局 Map 将元数据关联到一个对象
public class SocketManager { private Map<Socket,User> m = new HashMap<Socket,User>(); public void setUser(Socket s, User u) { m.put(s, u); } public User getUser(Socket s) { return m.get(s); } public void removeUser(Socket s) { m.remove(s); } } SocketManager socketManager; ... socketManager.setUser(socket, user);
这种方法的问题是元数据的生命周期需要与套接字的生命周期挂钩,但是除非准确地知道什么时候程序不再需要这个套接字,并记住从 Map
中删除相应的映射,否则,Socket
和 User
对象将会永远留在 Map
中,远远超过响应了请求和关闭套接字的时间。这会阻止 Socket
和 User
对象被垃圾收集,即使应用程序不会再使用它们。这些对象留下来不受控制,很容易造成程序在长时间运行后内存爆满。除了最简单的情况,在几乎所有情况下找出什么时候 Socket
不再被程序使用是一件很烦人和容易出错的任务,需要人工对内存进行管理。
找出内存泄漏
程序有内存泄漏的第一个迹象通常是它抛出一个 OutOfMemoryError
,或者因为频繁的垃圾收集而表现出糟糕的性能。幸运的是,垃圾收集可以提供能够用来诊断内存泄漏的大量信息。如果以 -verbose:gc
或者 -Xloggc
选项调用 JVM,那么每次 GC 运行时在控制台上或者日志文件中会打印出一个诊断信息,包括它所花费的时间、当前堆使用情况以及恢复了多少内存。记录 GC 使用情况并不具有干扰性,因此如果需要分析内存问题或者调优垃圾收集器,在生产环境中默认启用 GC 日志是值得的。
有工具可以利用 GC 日志输出并以图形方式将它显示出来,JTune 就是这样的一种工具(请参阅 参考资料)。观察 GC 之后堆大小的图,可以看到程序内存使用的趋势。对于大多数程序来说,可以将内存使用分为两部分:baseline 使用和 current load 使用。对于服务器应用程序,baseline 使用就是应用程序在没有任何负荷、但是已经准备好接受请求时的内存使用,current load 使用是在处理请求过程中使用的、但是在请求处理完成后会释放的内存。只要负荷大体上是恒定的,应用程序通常会很快达到一个稳定的内存使用水平。如果在应用程序已经完成了其初始化并且负荷没有增加的情况下,内存使用持续增加,那么程序就可能在处理前面的请求时保留了生成的对象。
清单 2 展示了一个有内存泄漏的程序。MapLeaker
在线程池中处理任务,并在一个 Map
中记录每一项任务的状态。不幸的是,在任务完成后它不会删除那一项,因此状态项和任务对象(以及它们的内部状态)会不断地积累。
清单 2. 具有基于 Map 的内存泄漏的程序
public class MapLeaker { public ExecutorService exec = Executors.newFixedThreadPool(5); public Map<Task, TaskStatus> taskStatus = Collections.synchronizedMap(new HashMap<Task, TaskStatus>()); private Random random = new Random(); private enum TaskStatus { NOT_STARTED, STARTED, FINISHED }; private class Task implements Runnable { private int[] numbers = new int[random.nextInt(200)]; public void run() { int[] temp = new int[random.nextInt(10000)]; taskStatus.put(this, TaskStatus.STARTED); doSomeWork(); taskStatus.put(this, TaskStatus.FINISHED); } } public Task newTask() { Task t = new Task(); taskStatus.put(t, TaskStatus.NOT_STARTED); exec.execute(t); return t; } }
图 1 显示 MapLeaker
GC 之后应用程序堆大小随着时间的变化图。上升趋势是存在内存泄漏的警示信号。(在真实的应用程序中,坡度不会这么大,但是在收集了足够长时间的 GC 数据后,上升趋势通常会表现得很明显。)
图 1. 持续上升的内存使用趋势

确信有了内存泄漏后,下一步就是找出哪种对象造成了这个问题。所有内存分析器都可以生成按照对象类进行分解的堆快照。有一些很好的商业堆分析工具,但是找出内存泄漏不一定要花钱买这些工具 —— 内置的 hprof
工具也可完成这项工作。要使用 hprof
并让它跟踪内存使用,需要以 -Xrunhprof:heap=sites
选项调用 JVM。
清单 3 显示分解了应用程序内存使用的 hprof
输出的相关部分。(hprof
工具在应用程序退出时,或者用 kill -3
或在 Windows 中按 Ctrl+Break 时生成使用分解。)注意两次快照相比,Map.Entry
、Task
和 int[]
对象有了显著增加。
请参阅 清单 3。
清单 4 展示了 hprof
输出的另一部分,给出了 Map.Entry
对象的分配点的调用堆栈信息。这个输出告诉我们哪些调用链生成了 Map.Entry
对象,并带有一些程序分析,找出内存泄漏来源一般来说是相当容易的。
清单 4. HPROF 输出,显示 Map.Entry 对象的分配点
TRACE 300446: java.util.HashMap$Entry.<init>(<Unknown Source>:Unknown line) java.util.HashMap.addEntry(<Unknown Source>:Unknown line) java.util.HashMap.put(<Unknown Source>:Unknown line) java.util.Collections$SynchronizedMap.put(<Unknown Source>:Unknown line) com.quiotix.dummy.MapLeaker.newTask(MapLeaker.java:48) com.quiotix.dummy.MapLeaker.main(MapLeaker.java:64)
弱引用来救援了
SocketManager
的问题是 Socket
-User
映射的生命周期应当与 Socket
的生命周期相匹配,但是语言没有提供任何容易的方法实施这项规则。这使得程序不得不使用人工内存管理的老技术。幸运的是,从 JDK 1.2 开始,垃圾收集器提供了一种声明这种对象生命周期依赖性的方法,这样垃圾收集器就可以帮助我们防止这种内存泄漏 —— 利用弱引用。
弱引用是对一个对象(称为 referent)的引用的持有者。使用弱引用后,可以维持对 referent 的引用,而不会阻止它被垃圾收集。当垃圾收集器跟踪堆的时候,如果对一个对象的引用只有弱引用,那么这个 referent 就会成为垃圾收集的候选对象,就像没有任何剩余的引用一样,而且所有剩余的弱引用都被清除。(只有弱引用的对象称为弱可及(weakly reachable)。)
WeakReference
的 referent 是在构造时设置的,在没有被清除之前,可以用 get()
获取它的值。如果弱引用被清除了(不管是 referent 已经被垃圾收集了,还是有人调用了 WeakReference.clear()
),get()
会返回 null
。相应地,在使用其结果之前,应当总是检查 get()
是否返回一个非 null 值,因为 referent 最终总是会被垃圾收集的。
用一个普通的(强)引用拷贝一个对象引用时,限制 referent 的生命周期至少与被拷贝的引用的生命周期一样长。如果不小心,那么它可能就与程序的生命周期一样 —— 如果将一个对象放入一个全局集合中的话。另一方面,在创建对一个对象的弱引用时,完全没有扩展 referent 的生命周期,只是在对象仍然存活的时候,保持另一种到达它的方法。
弱引用对于构造弱集合最有用,如那些在应用程序的其余部分使用对象期间存储关于这些对象的元数据的集合 —— 这就是 SocketManager
类所要做的工作。因为这是弱引用最常见的用法,WeakHashMap
也被添加到 JDK 1.2 的类库中,它对键(而不是对值)使用弱引用。如果在一个普通 HashMap
中用一个对象作为键,那么这个对象在映射从 Map
中删除之前不能被回收,WeakHashMap
使您可以用一个对象作为 Map
键,同时不会阻止这个对象被垃圾收集。清单 5 给出了 WeakHashMap
的 get()
方法的一种可能实现,它展示了弱引用的使用:
清单 5. WeakReference.get() 的一种可能实现
public class WeakHashMap<K,V> implements Map<K,V> { private static class Entry<K,V> extends WeakReference<K> implements Map.Entry<K,V> { private V value; private final int hash; private Entry<K,V> next; ... } public V get(Object key) { int hash = getHash(key); Entry<K,V> e = getChain(hash); while (e != null) { K eKey= e.get(); if (e.hash == hash && (key == eKey || key.equals(eKey))) return e.value; e = e.next; } return null; }
调用 WeakReference.get()
时,它返回一个对 referent 的强引用(如果它仍然存活的话),因此不需要担心映射在 while
循环体中消失,因为强引用会防止它被垃圾收集。WeakHashMap
的实现展示了弱引用的一种常见用法 —— 一些内部对象扩展 WeakReference
。其原因在下面一节讨论引用队列时会得到解释。
在向 WeakHashMap
中添加映射时,请记住映射可能会在以后“脱离”,因为键被垃圾收集了。在这种情况下,get()
返回 null
,这使得测试get()
的返回值是否为 null
变得比平时更重要了。
用 WeakHashMap 堵住泄漏
在 SocketManager
中防止泄漏很容易,只要用 WeakHashMap
代替 HashMap
就行了,如清单 6 所示。(如果 SocketManager
需要线程安全,那么可以用 Collections.synchronizedMap()
包装 WeakHashMap
)。当映射的生命周期必须与键的生命周期联系在一起时,可以使用这种方法。不过,应当小心不滥用这种技术,大多数时候还是应当使用普通的 HashMap
作为 Map
的实现。
清单 6. 用 WeakHashMap 修复 SocketManager
public class SocketManager { private Map<Socket,User> m = new WeakHashMap<Socket,User>(); public void setUser(Socket s, User u) { m.put(s, u); } public User getUser(Socket s) { return m.get(s); } }
引用队列
WeakHashMap
用弱引用承载映射键,这使得应用程序不再使用键对象时它们可以被垃圾收集,get()
实现可以根据 WeakReference.get()
是否返回 null
来区分死的映射和活的映射。但是这只是防止 Map
的内存消耗在应用程序的生命周期中不断增加所需要做的工作的一半,还需要做一些工作以便在键对象被收集后从 Map
中删除死项。否则,Map
会充满对应于死键的项。虽然这对于应用程序是不可见的,但是它仍然会造成应用程序耗尽内存,因为即使键被收集了,Map.Entry
和值对象也不会被收集。
可以通过周期性地扫描 Map
,对每一个弱引用调用 get()
,并在 get() 返回 null
时删除那个映射而消除死映射。但是如果 Map
有许多活的项,那么这种方法的效率很低。如果有一种方法可以在弱引用的 referent 被垃圾收集时发出通知就好了,这就是引用队列 的作用。
引用队列是垃圾收集器向应用程序返回关于对象生命周期的信息的主要方法。弱引用有两个构造函数:一个只取 referent 作为参数,另一个还取引用队列作为参数。如果用关联的引用队列创建弱引用,在 referent 成为 GC 候选对象时,这个引用对象(不是 referent)就在引用清除后加入 到引用队列中。之后,应用程序从引用队列提取引用并了解到它的 referent 已被收集,因此可以进行相应的清理活动,如去掉已不在弱集合中的对象的项。(引用队列提供了与 BlockingQueue
同样的出列模式 —— polled、timed blocking 和 untimed blocking。)
WeakHashMap
有一个名为 expungeStaleEntries()
的私有方法,大多数 Map
操作中会调用它,它去掉引用队列中所有失效的引用,并删除关联的映射。清单 7 展示了 expungeStaleEntries()
的一种可能实现。用于存储键-值映射的 Entry
类型扩展了 WeakReference
,因此当expungeStaleEntries()
要求下一个失效的弱引用时,它得到一个 Entry
。用引用队列代替定期扫描内容的方法来清理 Map
更有效,因为清理过程不会触及活的项,只有在有实际加入队列的引用时它才工作。
清单 7. WeakHashMap.expungeStaleEntries() 的可能实现
private void expungeStaleEntries() { Entry<K,V> e; while ( (e = (Entry<K,V>) queue.poll()) != null) { int hash = e.hash; Entry<K,V> prev = getChain(hash); Entry<K,V> cur = prev; while (cur != null) { Entry<K,V> next = cur.next; if (cur == e) { if (prev == e) setChain(hash, next); else prev.next = next; break; } prev = cur; cur = next; } } }
结束语
弱引用和弱集合是对堆进行管理的强大工具,使得应用程序可以使用更复杂的可及性方案,而不只是由普通(强)引用所提供的“要么全部要么没有”可及性。
While programs in the Java™ language are theoretically immune from "memory leaks," there are situations in which objects are not garbage collected even though they are no longer part of the program's logical state.
For garbage collection (GC) to reclaim objects no longer in use by the program, the logical lifetime of an object (the time that the application will use it) and the actual lifetime of outstanding references to that object must be the same. Most of the time, good software engineering techniques ensure that this happens automatically, without us having to expend a lot of attention on the issue of object lifetime. But every once in a while, we create a reference that holds an object in memory much longer than we expected it to, a situation referred to as unintentional object retention.
Memory leaks with global Maps
The most common source of unintentional object retention is the use of a Map
to associate metadata with transient objects. Let's say you have an object with an intermediate lifetime -- longer than that of the method call that allocated it, but shorter than that of the application -- such as a socket connection from a client. You want to associate some metadata with that socket, such as the identity of the user that has made the connection. You don't know this information at the time the Socket
is created, and you cannot add data to the Socket
object because you do not control the Socket
class or its instantiation. In this case, the typical approach is to store such information in a global Map
, as shown in the SocketManager
class in Listing 1:
Listing 1. Using a global Map to associate metadata with an object
public class SocketManager { private Map<Socket,User> m = new HashMap<Socket,User>(); public void setUser(Socket s, User u) { m.put(s, u); } public User getUser(Socket s) { return m.get(s); } public void removeUser(Socket s) { m.remove(s); } } SocketManager socketManager; ... socketManager.setUser(socket, user);
The problem with this approach is that the lifetime of the metadata needs to be tied to the lifetime of the socket, but unless you know exactly when the socket is no longer needed by the program and remember to remove the corresponding mapping from the Map
, the Socket
and User
objects will stay in the Map
forever, long after the request has been serviced and the socket has been closed. This prevents the Socket
and User
objects from being garbage collected, even though the application is never going to use either of them again. Left unchecked, this could easily cause a program to run out of memory if it runs for long enough. In all but the most trivial cases, the techniques for identifying when the Socket
becomes no longer used by the program resemble the annoying and error-prone techniques required for manual memory management.
Identifying memory leaks
The first sign that your program has a memory leak is usually that it throws an OutOfMemoryError
or starts to exhibit poor performance because of frequent garbage collection. Fortunately, the garbage collector is willing to share a lot of information that can be used to diagnose a memory leak. If you invoke the JVM with the -verbose:gc
or the -Xloggc
option, a diagnostic message is printed on the console or to a log file every time the GC runs, including how long it took, the current heap usage, and how much memory was recovered. Logging GC usage is not intrusive, and so it is reasonable to enable GC logging in production by default in the event you ever need to analyze memory problems or tune the garbage collector.
Tools can take the GC log output and display it graphically; one such tool is the free JTune (see Resources). By looking at the graph of heap size after GC, you can see the trend of your program's memory usage. For most programs, you can divide memory usage into two components: thebaseline usage and the current load usage. For a server application, the baseline usage is what the application uses when it is not subjected to any load but is ready to accept requests; the current load usage is what is used in the process of handling requests but released when the request processing is complete. So long as load is roughly constant, applications typically reach a steady state level of memory usage fairly quickly. If memory usage continues to trend upwards even though the application has completed its initialization and its load is not increasing, the program is probably retaining objects generated in the course of processing prior requests.
Listing 2 shows a program that has a memory leak. MapLeaker
processes tasks in a thread pool and records the status of each task in a Map
. Unfortunately, it never removes the entry when the task is finished, so the status entries and the task objects (along with their internal state) accumulate forever.
Listing 2. Program with a Map-based memory leak
public class MapLeaker { public ExecutorService exec = Executors.newFixedThreadPool(5); public Map<Task, TaskStatus> taskStatus = Collections.synchronizedMap(new HashMap<Task, TaskStatus>()); private Random random = new Random(); private enum TaskStatus { NOT_STARTED, STARTED, FINISHED }; private class Task implements Runnable { private int[] numbers = new int[random.nextInt(200)]; public void run() { int[] temp = new int[random.nextInt(10000)]; taskStatus.put(this, TaskStatus.STARTED); doSomeWork(); taskStatus.put(this, TaskStatus.FINISHED); } } public Task newTask() { Task t = new Task(); taskStatus.put(t, TaskStatus.NOT_STARTED); exec.execute(t); return t; } }
Figure 1 shows a graph of the application heap size after GC for MapLeaker
over time. The upward-sloping trend is a telltale sign of a memory leak. (In real applications, the slope is never this dramatic, but it usually becomes apparent if you gather GC data for long enough.)
Figure 1. Persistent upward memory usage trend

Once you are convinced you have a memory leak, the next step is to find out what type of objects are causing the problem. Any memory profiler can produce snapshots of the heap broken down by object class. There are some excellent commercial heap profiling tools available, but you don't have to spend any money to find memory leaks -- the built-in hprof
tool can also do the trick. To use hprof
and instruct it to track memory usage, invoke the JVM with the -Xrunhprof:heap=sites
option.
Listing 3 shows the relevant portion of the hprof
output breaking down the application's memory usage. (The hprof
tool produces a usage breakdown after the application exits, or when you signal your application with a kill -3
or by pressing Ctrl+Break on Windows.) Notice that there has been significant growth in Map.Entry
, Task
, and int[]
objects between the two snapshots.
See Listing 3.
Listing 4 shows yet another portion of the hprof
output, giving call stack information for allocation sites for Map.Entry
objects. This output tells us which call chains are generating the Map.Entry
objects; with some program analysis, it is usually fairly easy to pinpoint the source of the memory leak.
Listing 4. HPROF output showing allocation site for Map.Entry objects
TRACE 300446: java.util.HashMap$Entry.<init>(<Unknown Source>:Unknown line) java.util.HashMap.addEntry(<Unknown Source>:Unknown line) java.util.HashMap.put(<Unknown Source>:Unknown line) java.util.Collections$SynchronizedMap.put(<Unknown Source>:Unknown line) com.quiotix.dummy.MapLeaker.newTask(MapLeaker.java:48) com.quiotix.dummy.MapLeaker.main(MapLeaker.java:64)
Weak references to the rescue
The problem with SocketManager
is that the lifetime of the Socket
-User
mapping should be matched to the lifetime of the Socket
, but the language does not provide any easy means to enforce this rule. This forces the program to fall back on techniques that resemble manual memory management. Fortunately, as of JDK 1.2, the garbage collector provides a means to declare such object lifecycle dependencies so that the garbage collector can help us prevent this sort of memory leak -- with weak references.
A weak reference is a holder for a reference to an object, called the referent. With weak references, you can maintain a reference to the referent without preventing it from being garbage collected. When the garbage collector traces the heap, if the only outstanding references to an object are weak references, then the referent becomes a candidate for GC as if there were no outstanding references, and any outstanding weak references are cleared. (An object that is only referenced by weak references is called weakly reachable.)
The referent of a WeakReference
is set at construction time, and its value, if it has not yet been cleared, can be retrieved with get()
. If the weak reference has been cleared (either because the referent has already been garbage collected or because someone calledWeakReference.clear()
), get()
returns null
. Accordingly, you should always check that get()
returns a non-null value before using its result, as it is expected that the referent will eventually be garbage collected.
When you copy an object reference using an ordinary (strong) reference, you constrain the lifetime of the referent to be at least as long as that of the copied reference. If you are not careful, this can be the lifetime of your program -- such as when you place an object in a global collection. On the other hand, when you create a weak reference to an object, you do not extend the lifetime of the referent at all; you simply maintain an alternate way to reach it while it is still alive.
Weak references are most useful for building weak collections, such as those that store metadata about objects only for as long as the rest of the application uses those objects -- which is exactly what the SocketManager
class is supposed to do. Because this is such a common use for weak references, WeakHashMap
, which uses weak references for keys (but not for values), was also added to the class library in JDK 1.2. If you use an object as a key in an ordinary HashMap
, that object cannot be collected until the mapping is removed from the Map
; WeakHashMap
allows you to use an object as a Map
key without preventing that object from being garbage collected. Listing 5 shows a possible implementation of theget()
method from WeakHashMap
, showing the use of weak references:
Listing 5. Possible implementation of WeakReference.get()
public class WeakHashMap<K,V> implements Map<K,V> { private static class Entry<K,V> extends WeakReference<K> implements Map.Entry<K,V> { private V value; private final int hash; private Entry<K,V> next; ... } public V get(Object key) { int hash = getHash(key); Entry<K,V> e = getChain(hash); while (e != null) { K eKey= e.get(); if (e.hash == hash && (key == eKey || key.equals(eKey))) return e.value; e = e.next; } return null; }
When WeakReference.get()
is called, it returns a strong reference to the referent (if it is still alive), so there is no need to worry about the mapping disappearing in the body of the while
loop because the strong reference keeps it from being garbage collected. The implementation ofWeakHashMap
illustrates a common idiom with weak references -- that some internal object extends WeakReference
. The reason for this becomes clear in the next section when we discuss reference queues.
When you add a mapping to a WeakHashMap
, remember that it is possible that the mapping could "fall out" later because the key is garbage collected. In that case, get()
returns null
, making it even more important than usual to test the return value of get()
for null
.
Plugging the leak with WeakHashMap
Fixing the leak in SocketManager
is easy; simply replace the HashMap
with a WeakHashMap
, as shown in Listing 6. (If SocketManager
needs to be thread-safe, you can wrap the WeakHashMap
with Collections.synchronizedMap()
). You can use this approach whenever the lifetime of the mapping must be tied to the lifetime of the key. However, you should be careful not to overuse this technique; most of the time an ordinaryHashMap
is the right Map
implementation to use.
Listing 6. Fixing SocketManager with WeakHashMap
public class SocketManager { private Map<Socket,User> m = new WeakHashMap<Socket,User>(); public void setUser(Socket s, User u) { m.put(s, u); } public User getUser(Socket s) { return m.get(s); } }
Reference queues
WeakHashMap
uses weak references for holding map keys, which allows the key objects to be garbage collected when they are no longer used by the application, and the get()
implementation can tell a live mapping from a dead one by whether WeakReference.get()
returns null
. But this is only half of what is needed to keep a Map
's memory consumption from increasing throughout the lifetime of the application; something must also be done to prune the dead entries from the Map
after the key object has been collected. Otherwise, the Map
would simply fill up with entries corresponding to dead keys. And while this would be invisible to the application, it could still cause the application to run out of memory because the Map.Entry
and value objects would not be collected, even if the key is.
Dead mappings could be eliminated by periodically scanning the Map
, calling get()
on each weak reference, and removing the mapping if get() returns null
. But this would be inefficient if the Map
has many live entries. It would be nice if there was a way to be notified when the referent of a weak reference is garbage collected, and that is what reference queues are for.
Reference queues are the garbage collector's primary means of feeding back information to the application about object lifecycle. Weak references have two constructors: one takes only the referent as an argument and the other also takes a reference queue. When a weak reference has been created with an associated reference queue and the referent becomes a candidate for GC, the reference object (not the referent) is enqueued on the reference queue after the reference is cleared. The application can then retrieve the reference from the reference queue and learn that the referent has been collected so it can perform associated cleanup activities, such as expunging the entries for objects that have fallen out of a weak collection. (Reference queues offer the same dequeuing modes as BlockingQueue
-- polled, timed blocking, and untimed blocking.)
WeakHashMap
has a private method called expungeStaleEntries()
that is called during most Map
operations, which polls the reference queue for any expired references and removes the associated mappings. A possible implementation of expungeStaleEntries()
is shown in Listing 7. The Entry
type, which is used to store the key-value mapping, extends WeakReference
, so when expungeStaleEntries()
asks for the next expired weak reference, it gets back an Entry
. Using reference queues to clean up the Map
instead of periodically trawling through its contents is more efficient because live entries are never touched in the cleanup process; it only does work if there are actually enqueued references.
Listing 7. Possible implementation of WeakHashMap.expungeStaleEntries()
private void expungeStaleEntries() { Entry<K,V> e; while ( (e = (Entry<K,V>) queue.poll()) != null) { int hash = e.hash; Entry<K,V> prev = getChain(hash); Entry<K,V> cur = prev; while (cur != null) { Entry<K,V> next = cur.next; if (cur == e) { if (prev == e) setChain(hash, next); else prev.next = next; break; } prev = cur; cur = next; } } }
Conclusion
Weak references and weak collections are powerful tools for heap management, allowing the application to use a more sophisticated notion of reachability, rather than the "all or nothing" reachability offered by ordinary (strong) references.