0
07-26 20:59:09.464: ERROR/dalvikvm-heap(5530): Out of memory on a 87396-byte allocation.
 07-26 20:59:09.512: INFO/dalvikvm(5530): "AsyncTask #1" prio=5 tid=9 RUNNABLE
  07-26 20:59:09.512: INFO/dalvikvm(5530):   | group="main" sCount=0 dsCount=0        obj=0x40510718 self=0x29ffc8
   07-26 20:59:09.512: INFO/dalvikvm(5530):   | sysTid=5542 nice=10 sched=0/0 cgrp=bg_non_interactive handle=2752768
   07-26 20:59:09.561: INFO/dalvikvm(5530):   | schedstat=( 2745693669 7741199384 330 )

This is the error i get when running this method in my asyncTask.

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");


              Log.v("getImage1", "Retreived image");
            }
            imageUrl = total.toString();
     }
android_king22
  • 759
  • 2
  • 20
  • 36
  • What does ddms say about the state of the heap before the allocation happens? What line is this allocation related to? – Dan S Jul 26 '11 at 21:36
  • 1
    if you're using StringBuilder you shouldn't use + operand on Strings, that kills its purpose. – Klaimmore Jul 26 '11 at 21:50

1 Answers1

0

The url in your code (https://sites.google.com/site/theitrangers/images/webImages.txt) resolves to a 61 byte string https://sites.google.com/site/theitrangers/images/ncaa12.jpg. So this is probably not the main drain on your heap. It is possible that you have a lot of other things using up your memory and this just happens to be where things tip over. Anyways the OOM looks like it is happening when you allocate your BufferedReader. The size of the buffer is actually bigger than the size of the content (aforementioned 61 byte string). You can specify a smaller buffer in BufferedReader's constructor. You don't really need a buffer for a 61 byte entity anyways. Similarly you probably don't need to use BufferedHttpEntity. You can go even further in trimming down the memory being used here, but you've got something else in your app using up a lot more memory.

michaelg
  • 2,692
  • 1
  • 17
  • 16