When OutofMemory error occurs in Android? : I am parsing JSON String fetched from web.(Around 40,000 records in a single chunk). I call AsyncTask recursively(40k records retrieved in a single call) until i fetched all the records from server(Total records 2 lac).

In first two calls app responds smooth and around 80k records fetched from server, but in third call OutOfMemory error occurs. I searched the web and got to know that this error occurs when heap size overflowed.

    String response = "";
                try {
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpEntity httpEntity = null;
                    HttpResponse httpResponse = null;

                    HttpPost httpPost = new HttpPost("http://URL");                                

                    httpPost.setEntity(new StringEntity(strings[0]));

                    httpPost.setHeader("Content-Type", "application/json");
                    httpPost.setHeader("Accept", "application/json");


                    httpResponse = httpClient.execute(httpPost);
                    httpEntity = httpResponse.getEntity();
                    response = EntityUtils.toString(httpEntity);

Error occurs on the below line.

    response = EntityUtils.toString(httpEntity);

My code works fine for first 2 calls, then why it throws OutOfMemory in next call? Why first two calls works fine with the same size of data? 

I know their are many questions regarding  OutOfMemory error. But, I want to know that how android allocates the heap size? Does it depends on the free memory of device? Does the size of heap reduced by android when the size of app data increase or it remains same?

Please ignore if you found this question worthless. :) 