I am parsing a JSON String fetched from the internet (Around 40,000 records in a single request). I am call AsyncTask recursively until I have fetched all the records from server (Total records 200k).
In the first two calls app the responds smooth with around 80k records fetched from server, but in third call an OutOfMemoryException
occurs. I searched the web and got to know that this error occurs when the heap size overflows.
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);
The error occurs on the below line.
response = EntityUtils.toString(httpEntity);
My code works fine for first 2 calls, so why does it throw OutOfMemoryExcption
in the next call? Why is the first two calls working with the same size of data?
I know there are many questions regarding OutOfMemoryException
, but I want to know that how Android allocates the size of the heap? Does it depends on the free memory of device? Does the size of heap reduce when the size of app data increase or does it remains same?
My Code:
public void startBackgrounSync(){
final JSONObject jsonObject = new JSONObject();
try{
jsonObject.put("token","dssdf");
jsonObject.put("api_key","fdsf");
jsonObject.put("org_id","101");
jsonObject.put("sync_type","background_sync");
jsonObject.put("start",Integer.toString(start));
jsonObject.put("total",Integer.toString(limit));
}catch (JSONException e){
e.printStackTrace();
}
AsyncTask<String, Integer, String> backGroundSync = new AsyncTask<String, Integer, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
System.out.println("BackGround Sync Process Start: ");
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);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("Response: ", "> " + response);
return response;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try{
JSONObject jsonObject1 = new JSONObject(s);
JSONObject data = jsonObject1.getJSONObject("data");
if(data != null){
if (data.getBoolean("is_success")){
JSONObject alumni_data = data.getJSONObject("alumni_data");
JSONArray alumni_profile_data = alumni_data
.getJSONArray("alumni_profile_data");
ContentValues[] values = new ContentValues[alumni_profile_data.length()];
if (alumni_profile_data == null){
Toast.makeText(MainActivity.this,"Sync complete.....",Toast.LENGTH_LONG).show();
}else{
JSONObject alumini_object;
for (int i = 0; i < alumni_profile_data.length(); i++) {
alumini_object = alumni_profile_data.getJSONObject(i);
String id = alumini_object.getString("id");
String org_id = alumini_object.optString("org_id");
ContentValues contentValues = new ContentValues();
contentValues.put("id", id);
contentValues.put("org_id", org_id);
contentValues.put("profile_photo", profile_photo);
values[i] = contentValues;
}
}
String uri = "content://com.poras.provider.alumni/data";
Uri uri1 = Uri.parse(uri);
int length = getApplicationContext().getContentResolver().bulkInsert(uri1,values);
System.out.println("Length of rows...... " +length);
start = start+50000;
values = new ContentValues[0];
startBackgrounSync();// Calling Method Recursively
}
}
}catch (JSONException e){
e.printStackTrace();
}catch (OutOfMemoryError error){
System.out.println("Out of memory exception...... "+ error);
}
}
}
};
backGroundSync.execute(jsonObject.toString());
}