排球笔记上的注释以避免泄漏

A annotation on the volley note to avoid leaking

CacheDispatcher 的代码如下。并专注于释放先前的请求对象,以避免在耗尽 mQueue 时泄漏请求对象。 我不知道为什么。谁能告诉我原因? 提前致谢。

java
Request<?> request;
while (true) {
    // release previous request object to avoid leaking request object when mQueue is drained.
    request = null;
    try {
        // Take a request from the queue.
        request = mCacheQueue.take();
    } catch (InterruptedException e) {
        // We may have been interrupted because it was time to quit.
        if (mQuit) {
            return;
        }
        continue;
    }
}

request = mCacheQueue.take(); 是一个阻塞调用,所以当队列为空(耗尽)时,如果我们不执行 request = null;

,我们仍然会在等待时保留对旧请求的引用

然而,这并不是实现此行为的最佳方式,它在新的 Volley 代码中已更改为:

while (true) {
            try {
                // Get a request from the cache triage queue, blocking until
                // at least one is available.
                final Request<?> request = mCacheQueue.take();