Search code examples
androidmemory-leaksandroid-volleyleakcanary

A annotation on the volley note to avoid leaking


The code on the CacheDispatcher is as below. And focus on that release previous request object to avoid leaking request object when mQueue is drained. I do not know why. Can anyone tell me the reason? Thanks in advance.

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;
    }
}

Solution

  • request = mCacheQueue.take(); is a blocking call so when the queue is empty(drained) we will still keep reference of the old request while waiting if we didn't do request = null;

    this however is not the best way to achieve this behavior and it was changed in the new Volley code to :

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