Anish
Anish

Reputation: 785

Out of memory error when large xml present

Following is my code to get XML from server but when a XML is large, I am getting out of memory error Can you please tell me what i need to change in my code

    @Override
    protected String doInBackground(String... arg0) {
        String result = "";

        HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(getString(R.string.requesturl));
        // Execute the request
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);

            // Get hold of the response entity
            HttpEntity entity = response.getEntity();

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity());
            }

        } catch (Exception e) {
        }
        return result;
    }

    @Override
    protected void onPostExecute(String result ) 
    {
     progressDialog.dismiss();
     ParseResponse parse=new ParseResponse();
     InputSource is = new InputSource(new StringReader(result));
     Gesmes gesmes=parse.parseXML(is);
             }

It seems to work fine when XML is small and here is my Logcat:

   03-31 15:01:25.547: E/AndroidRuntime(9791): java.lang.OutOfMemoryError
    03-31 15:01:25.547: E/AndroidRuntime(9791):     at      java.util.ArrayList.add(ArrayList.java:118)
    03-31 15:01:25.547: E/AndroidRuntime(9791):     at  com.parser.ParseResponse.parseXML(ParseResponse.java:64)
   03-31 15:01:25.547: E/AndroidRuntime(9791):  at com.europeanexchangerates.MainActivity$AsyncCurrency.onPostExecute(MainActivity.java:426)
   03-31 15:01:25.547: E/AndroidRuntime(9791):  at com.europeanexchangerates.MainActivity$AsyncCurrency.onPostExecute(MainActivity.java:1)
        03-31 15:01:25.547: E/AndroidRuntime(9791):     at android.os.AsyncTask.finish(AsyncTask.java:632)
      03-31 15:01:25.547: E/AndroidRuntime(9791):   at android.os.AsyncTask.access$600(AsyncTask.java:177)
        03-31 15:01:25.547: E/AndroidRuntime(9791):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
       03-31 15:01:25.547: E/AndroidRuntime(9791):  at android.os.Handler.dispatchMessage(Handler.java:102)
       03-31 15:01:25.547: E/AndroidRuntime(9791):  at android.os.Looper.loop(Looper.java:136)
        03-31 15:01:25.547: E/AndroidRuntime(9791):     at android.app.ActivityThread.main(ActivityThread.java:5017)
       03-31 15:01:25.547: E/AndroidRuntime(9791):  at java.lang.reflect.Method.invokeNative(Native Method)
        03-31 15:01:25.547: E/AndroidRuntime(9791):     at java.lang.reflect.Method.invoke(Method.java:515)
       03-31 15:01:25.547: E/AndroidRuntime(9791):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
       03-31 15:01:25.547: E/AndroidRuntime(9791):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

Upvotes: 0

Views: 878

Answers (4)

Sethu
Sethu

Reputation: 428

Try android:largeHeap="true" to increase heap size for your app in your menifest. Some devices have fixed amount of memory used by app say 64 MB per app. Or use scaling and caching to fix the memory used my application. have look at these links http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

How to increase heap size of an android application?

thank you.

Upvotes: 0

Kiran Kumar
Kiran Kumar

Reputation: 1212

Instead of using largeHeap="true" you should consider using a SAX parser to parse XML. A DOM parser will eventually lead to out of memory error for large xmls and makes parsing very slow for large files.

Upvotes: 0

Naveen Kumar Kuppan
Naveen Kumar Kuppan

Reputation: 1442

For this type of error you add this line into your mainfest code it may take more memory to your application...

 <application
    android:largeHeap="true" 
                       >
  ..........
   ................
 </appliaction>

Upvotes: 0

Rino
Rino

Reputation: 743

Try to put

largeHeap:"true"

in your manifest.

Consider to test it not in a emulator, but in a real device.

Also consider to reduce the size of you xml.

Upvotes: 1

Related Questions