Volley slow and causing memory leak

前端 未结 3 1323
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 13:08

In my project, I am using volley to download a JSON stream which I parse and show in a listview. I use the following method to load my data:

private void lo         


        
相关标签:
3条回答
  • 2021-01-03 13:26

    I'm trying explain with few code (already Itai is explained in brief)

    Below code explains how to use the global request queue for Volley with a singleton instance of the application class for easy access in other places and make the code more memory efficient!.

    public class ApplicationController extends Application {

    /**
     * Log or request TAG
     */
    public static final String TAG = "VolleyPatterns";
    
    /**
     * Global request queue for Volley
     */
    private RequestQueue mRequestQueue;
    
    /**
     * A singleton instance of the application class for easy access in other places
     */
    private static ApplicationController sInstance;
    
    @Override
    public void onCreate() {
        super.onCreate();
    
        // initialize the singleton
        sInstance = this;
    }
    
    /**
     * @return ApplicationController singleton instance
     */
    public static synchronized ApplicationController getInstance() {
        return sInstance;
    }
    
    /**
     * @return The Volley Request queue, the queue will be created if it is null
     */
    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;
    }
    
    /**
     * Adds the specified request to the global queue, if tag is specified
     * then it is used else Default TAG is used.
     * 
     * @param req
     * @param tag
     */
    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    
        VolleyLog.d("Adding request to queue: %s", req.getUrl());
    
        getRequestQueue().add(req);
    }
    
    /**
     * Adds the specified request to the global queue using the Default TAG.
     * 
     * @param req
     * @param tag
     */
    public <T> void addToRequestQueue(Request<T> req) {
        // set the default tag if tag is empty
        req.setTag(TAG);
    
        getRequestQueue().add(req);
    }
    
    /**
     * Cancels all pending requests by the specified TAG, it is important
     * to specify a TAG so that the pending/ongoing requests can be cancelled.
     * 
     * @param tag
     */
    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    } }
    

    for the complete implementation ref this blog

    Credits: Arnab Chakraborty

    0 讨论(0)
  • 2021-01-03 13:46

    Every call to the method you create a new RequestQueue which is not a recommended approach. You should create one RequestQueue, probably a publicly visible singleton that is initialized once when the app is created.

    Try moving the RequestQueue outside and see if it solves your problem.

    0 讨论(0)
  • 2021-01-03 13:46

    i have some problem with:

    Volley.newRequestQueue(getApplicationContext());
    

    it caused the first request at startup to take awfully long time in some phones. i changed it to:

    Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024);
    Network network = new BasicNetwork(new HurlStack());
    mRequestQueue = new RequestQueue(cache, network);
    mRequestQueue.start();
    

    That solved my issue.

    0 讨论(0)
提交回复
热议问题