字节分配时出现内存不足错误?

时间:2011-07-26 21:08:11

标签: android memory

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 )

这是我在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();
     }

1 个答案:

答案 0 :(得分:0)

代码中的网址(https://sites.google.com/site/theitrangers/images/webImages.txt)解析为61字节字符串https://sites.google.com/site/theitrangers/images/ncaa12.jpg。所以这可能不是你堆上的主要消耗。有可能你有很多其他东西耗尽了你的记忆,这恰好是事情的结果。无论如何,当您分配BufferedReader时,OOM看起来就像是在发生。缓冲区的大小实际上大于内容的大小(前面提到的61字节字符串)。您可以在BufferedReader's constructor中指定较小的缓冲区。无论如何,你真的不需要一个61字节实体的缓冲区。同样,您可能不需要使用BufferedHttpEntity。你可以更进一步减少这里使用的内存,但你的应用程序中还有其他东西占用了更多的内存。