Naskov
Naskov

Reputation: 4179

Out Of Memory String Android

I am receiving huge JSON and and while I am reading the lines OutOfMemoryError appears.

Here is my first method that I am trying to parse the JSON.

    InputStream in = response.getEntity().getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
    String result = "";

    while (true) {

                String ss = reader.readLine();
                if (ss == null) {
                        break;
                }
                result += ss;
   }

And I've also tried this method.

    InputStream in = response.getEntity().getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;

    while ( (line = reader.readLine()) != null)
    {
    sb.append(line);
    }

In the both of the cases the OutOfMemoryErorr is appearing.

Upvotes: 5

Views: 6011

Answers (4)

Siddharth
Siddharth

Reputation: 9574

My JSON code was waiting for status, which comes towards the end. So I modified the code to return earlier.

// try to get formattedAddress without reading the entire JSON
        String formattedAddress;
        while ((read = in.read(buff)) != -1) {
            jsonResults.append(buff, 0, read);
            formattedAddress = ((String) ((JSONObject) new JSONObject(
                    jsonResults.toString()).getJSONArray("results").get(0))
                    .get("formatted_address"));
            if (formattedAddress != null) {
                Log.i("Taxeeta", "Saved memory, returned early from json") ;
                return formattedAddress;
            }               
        }
JSONObject statusObj = new JSONObject(jsonResults.toString());
        String status = (String) (statusObj.optString("status"));
        if (status.toLowerCase().equals("ok")) {
            formattedAddress = ((String) ((JSONObject) new JSONObject(
                    jsonResults.toString()).getJSONArray("results").get(0))
                    .get("formatted_address"));
            if (formattedAddress != null) {
                Log.w("Taxeeta", "Did not saved memory, returned late from json") ;
                return formattedAddress;
            }           
        } 

Upvotes: 1

Naskov
Naskov

Reputation: 4179

The best solution I found was to raise the Heap of the Application.

I placed android:largeHeap="true" under the <application/> in the AndroidManifest.xml.

Upvotes: 3

Raja Asthana
Raja Asthana

Reputation: 2100

Mostly the error will occur due to heap size. You need to increase the size of the heap

To increase the heap size

For additional info on heap size in java visit here

Upvotes: 3

Steven Byle
Steven Byle

Reputation: 13269

When you instantiate your BufferedReader, you pass in an int size of 8, meaning your buffer is 8 characters (I assume that's not what you want). If you pass no size, the default is 8192 characters.

Try this:

BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));

Or this (same effect):

BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8192);

http://developer.android.com/reference/java/io/BufferedReader.html

I'm not completely sure that will fix the error you are seeing, but worth trying if you can't get it working.

Upvotes: 2

Related Questions