且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

出在字节分配内存不足的错误?

更新时间:2023-01-14 14:21:15

在您的code的URL(https://sites.google.com/site/theitrangers/images/webImages.txt)解析为61字节串 https://sites.google.com/site/theitrangers/images/ncaa12 .JPG 。因此,这可能不是你堆的主要排水。这可能是你使用你的内存有很多其他的事情,这恰好是哪里的东西翻倒。不管怎样的OOM看起来,当你分配你的BufferedReader正在发生的事情。缓冲区的大小实际上比内容(上述61字节串)的尺寸更大。您可以指定一个较小的缓冲区 BufferedReader类的构造函数。你并不真的需要一个不管怎么说字节61实体的缓冲区。同样你也许并不需要使用BufferedHttpEntity。您可以占用大量的内存走得更远在修剪下来这里所使用的内存,但你已经得到的东西在你的应用程序别人。

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();
     }

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.