【问题标题】:Volley Queue Throwing OutOfMemoryError “pthread_createVolley 队列抛出 OutOfMemoryError “pthread_create
【发布时间】:2018-06-25 18:13:01
【问题描述】:

每次我提出请求时,我都会创建一个新的 Volley 请求队列:

public void initializeQueue(Context context){

    File cacheDir = new File(context.getCacheDir(), "Volley");
    this.queue = new RequestQueue(new DiskBasedCache(cacheDir), new BasicNetwork(new HurlStack()), MAX_SIZE_THREAD_POOL);
    this.queue.start();
}

当我完成我的请求时,我会清理变量

this.queue = null;

但是当我发出很多请求时,会抛出一个错误“Throwing OutOfMemoryError “pthread_create”。于是上网一搜,结论是只发起一次队列。

但我的问题是,如果我不断创建新的队列,为什么内存会不断增加?我使变量可以为空,因此旧队列无法访问并且 GC 可收集。还有其他东西在排队吗?

注意 上面的上下文使用了应用程序。

谢谢

【问题讨论】:

标签: android android-volley


【解决方案1】:

我对这个问题的有根据的猜测是:

背景:
您正在将应用程序上下文传递给队列。所以它将保持引用直到应用程序处于活动状态。

代码

this.queue = null;

因为这段代码只会清除它持有的引用,而不是它占用的内存。从内存中清除它的工作 GC 并且您无法保证 GC 何时会调用。

为什么要为每个请求创建一个新的 RequestQueue。你可以检查null。创建队列并使用相同的队列进行进一步处理。

File cacheDir = new File(context.getCacheDir(), "Volley");
if( this.queue == null ){
   this.queue = new RequestQueue(new DiskBasedCache(cacheDir), new BasicNetwork(new HurlStack()), MAX_SIZE_THREAD_POOL);
}

资源:- Understanding context, Understanding reference

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    相关资源
    最近更新 更多