android - Volley.newRequestQueue 导致 OutOfMemoryError

标签 android caching out-of-memory android-volley

我正在使用 Volley 设置图像 url。我的代码在以下位置崩溃:

mrq=Volley.newRequestQueue(this);

Log cat 表示豁免位于:com.android.volley.toolbox.DiskBasedCache.streamToBytes

如果我将代码注释掉,程序不会崩溃。

我已经尝试重启我的手机几次,因为在我的研究中我发现这对一些遇到这个问题的人有效。

为什么创建一个 RequestQueue 使用这么多内存?

如何防止发生 OutOfMemoryError?

我需要清空缓存吗?

感谢您的帮助并花时间阅读本文。

最佳答案

如何初始化 RequestQueue?我怀疑您正在为每个 Activity 创建 RequestQueues。因此在您的 Application 类中将其启动为

    public class ApplicationController extends Application {
        private static ApplicationController sInstance;
        private RequestQueue mRequestQueue;

        @Override
    public void onCreate() {
        super.onCreate();

        // initialize the singleton
        sInstance = this;
    }

 public static synchronized ApplicationController getInstance() {
        return sInstance;
    }



        public RequestQueue getRequestQueue() {
                // lazy initialize the request queue, the queue instance will be
                // created when it is accessed for the first time
                if (mRequestQueue == null) {
                    mRequestQueue = Volley.newRequestQueue(getApplicationContext());
                }

                return mRequestQueue;
            }


    //your code


    }

然后从您的 Activity 中获取请求队列作为

mrq = ApplicationController.getInstance().getRequestQueue();

关于android - Volley.newRequestQueue 导致 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24021411/

相关文章:

android - 单击搜索图标时如何在工具栏中使用 SearchView

caching - 缓存激活时地理服务器层会移位

java - 使用 iText 创建 10000 多页 PDF

java - 从列表中删除重复项而不将列表存储在内存中

asp.net-mvc - 在 ASP.NET MVC 5 中使用远程 Redis 配置 OutputCache

android - 特定 Activity 被销毁时释放内存

android - 如何在 Windows 操作系统上安装 Android NDK?没有任何 Unix Linux 的简单方法?

Android 视频不适合视频 View 的纵向宽度并且不占用横向全屏

java - Android 上的 Lucene 4 错误

javascript - 哪些浏览器在缓存 XMLHTTPRequest 响应时有问题?