yoshi24
yoshi24

Reputation: 3177

Why am I getting an out of memory error?

I keep getting this error?

07-25 17:04:00.796: ERROR/AndroidRuntime(420): Caused by: java.lang.OutOfMemoryError
07-25 17:04:00.796: ERROR/AndroidRuntime(420):     at org.apache.http.impl.io.AbstractSessionInputBuffer.init(AbstractSessionInputBuffer.java:79)
07-25 17:04:00.796: ERROR/AndroidRuntime(420):     at org.apache.http.impl.SocketHttpClientConnection.createSessionInputBuffer(SocketHttpClientConnection.java:83)

Everytime I try to run a this method:

public void getImages() throws IOException{


    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpGet httppost = new HttpGet("https://sites.google.com/site/theitrangers/images/webImages.txt");
    HttpResponse response;

        response = httpclient.execute(httppost);


            HttpEntity ht = response.getEntity();

            BufferedHttpEntity buf = new BufferedHttpEntity(ht);

            InputStream is = buf.getContent();


            BufferedReader r = new BufferedReader(new InputStreamReader(is));

            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line + "\n");

              imageUrl = total.toString();
              Log.v("getImage1", "Retreived image");
            }
     }

All this method is doing is retreiving URL from a text file hosted on a website.

EDIT: LINE OF CODE WHERE DEBUG IS POINTING ME TO. WHEN IT RUNS I GET OUT OF MEMORY ERROR.

 public void getImages() throws IOException{


    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpGet httppost = new HttpGet("https://sites.google.com/site/theitrangers/images/webImages.txt");
    HttpResponse response;

**Specifically here:**  response = httpclient.execute(httppost);

Upvotes: 3

Views: 2651

Answers (2)

Marc Van Daele
Marc Van Daele

Reputation: 2734

If there is no leak in your application, then this is probably the same issue as discussed (and fixed!) in Android HttpClient OOM on 4G/LTE (HTC Thunderbolt)

Upvotes: 0

Maurício Linhares
Maurício Linhares

Reputation: 40313

You're possibly getting the OOM `cos you're loading that text file, that should be large, in memory on that StringBuilder. You should write this file to a local file instead of the StringBuilder to avoid the error.

Upvotes: 3

Related Questions