3

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);
Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
yoshi24
  • 3,147
  • 8
  • 45
  • 62
  • Please search for similar problems. I am sure you will get quite a few similar threads where people have already discussed this issue. – Kumar Bibek Jul 25 '11 at 17:51
  • Could you post the complete stack trace? Tough to nail down the exact problematic line in your code. Definitely a memory leak though. – Sagar Hatekar Jul 25 '11 at 19:54
  • @Sagar Hatekar Posted where the debug is pointing to above in the edit. – yoshi24 Jul 25 '11 at 21:44
  • `VM` doesnt hold anything against you. You are getting an OOM because your program must have ran out of memory. :-P – Mukul Goel Nov 05 '12 at 10:16

2 Answers2

3

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.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
  • okay so maybe i could have the code that fetches the string from the text doc. Stored inside of a SQLite database and have it pull from there. The only problem is i will be changing the url's often. And i dont want to keep storing urls in a database having it grow in size for old URLs. – yoshi24 Jul 25 '11 at 21:53
  • You can just clean the database on every import. – Maurício Linhares Jul 25 '11 at 21:55
  • 1
    The content of the referenced file is simply: `https://sites.google.com/site/theitrangers/images/ncaa12.jpg `. Certainly not large enough for an OOM, not even on my wristwatch. – Lawrence Dol Jul 25 '11 at 21:56
  • Well you... Its just a simple text URL. So what do you guys think it could be? When i run it on my real life debug device it runs great – yoshi24 Jul 25 '11 at 21:57
  • Check the memory limit for the VM you're using, maybe it's too low. – Maurício Linhares Jul 25 '11 at 21:59
  • I ran it on my device and so everytime the i swipe to view the different images fetched, when image is off the screen in swipped back on it fetched the images all over again. using wayyyy to much cpu. causing a ANR. – yoshi24 Jul 25 '11 at 22:06
  • Are you familiar with the Droi Fu library? – yoshi24 Jul 25 '11 at 22:12
0

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)

Community
  • 1
  • 1
Marc Van Daele
  • 2,856
  • 1
  • 26
  • 52