1

My app downloads data from a PHP Server using threads from an ExecutorService with ThreadPool size 4. In API 27, the app runs perfectly without causing any OOM exception from the Heap memory. But all the previous API <26 throws OOM.

Below is a simple code example that executes 134 requests and then adds the response to the pool. In API 27 the maximum Heap size, reaches 68 mb. But when i run it with API 19,21,25 e.t.c the maximum Heap Size reaches 123 mb.

When i remove the Log.d("Process response", (response)+""); in API 27 the Heap Size goes down to 10mb. But with API 19, the Heap Size goes to 60mb.

I cant understand why it doubles the size. It looks like in API 19 and above increases memory when adding tasks in the pool and also when holding the reference in the response variable.

Any suggestions?

Note The thread sleep is for the example only to see the maximum Heap size

Thanks

private ActivityManager activityManager;
ExecutorService service  = Executors.newFixedThreadPool(4);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    activityManager= (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);;

    final  RequestQueue queue = Volley.newRequestQueue(this);
    String url =".....";
    for(int i =0 ; i<134 ; i++) {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        method(response);  //0.5mb
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("VolleyError", error.toString());
            }
        });
        queue.add(stringRequest);
    }

}


public void method(final String response){
        Runnable task = new Runnable() {
            @Override
            public void run() {
                try {
                    Log.d("Process response", (response)+"");
                    Thread.sleep(200000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        logHeap("");
        service.execute(task);
}

closed as off-topic by Blrfl, gnat, Thomas Owens Jul 1 '18 at 23:43

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for assistance in explaining, writing or debugging code are off-topic here. These can be asked on Stack Overflow if they include the desired behavior, a specific problem or error, and the shortest code necessary to reproduce it in the question (see Minimal, Complete, and Verifiable Example)." – gnat, Thomas Owens
If this question can be reworded to fit the rules in the help center, please edit the question.