出在字节分配内存不足的错误? [英] Out of memory error on byte allocation?

查看:185
本文介绍了出在字节分配内存不足的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  20 07-26:59:09.464:ERROR / dalvikvm堆(5530):内存在一个87396字节的分配。
 07-26 20:59:09.512:信息/ dalvikvm(5530):AsyncTask的#1PRIO = 5 TID = 9 RUNNABLE
  07-26 20:59:09.512:信息/ dalvikvm(5530)|组=主SCOUNT = 0 dsCount = 0 = OBJ自我0x40510718 = 0x29ffc8
   07-26 20:59:09.512:信息/ dalvikvm(5530)| sysTid = 5542 =不错附表10 = 0/0 = CGRP手柄bg_non_interactive = 2752768
   07-26 20:59:09.561:信息/ dalvikvm(5530)| schedstat =(2745693669 7741199384 330)

这是错误在我的AsyncTask运行此方法时,我得到。

 公共无效getImages()抛出IOException
    DefaultHttpClient的HttpClient =新DefaultHttpClient();    HTTPGET httppost =新HTTPGET(https://sites.google.com/site/theitrangers/images/webImages.txt);
    HTT presponse响应;        响应= httpclient.execute(httppost);
            HttpEntity HT = response.getEntity();            BufferedHttpEntity BUF =新BufferedHttpEntity(HT);            InputStream为= buf.getContent();
            BufferedReader中R =新的BufferedReader(新的InputStreamReader(是));            StringBuilder的总=新的StringBuilder();
            串线;
            而((行= r.readLine())!= NULL){
                total.append(行+\\ n);
              Log.v(getImage1,Retreived图像);
            }
            图片网址= total.toString();
     }


解决方案

在您的code的URL(https://sites.google.com/site/theitrangers/images/webImages.txt)解析为61字节串 https://sites.google.com/site/theitrangers/images/ncaa12 .JPG 。因此,这可能不是你堆的主要排水。这可能是你使用你的内存有很多其他的事情,这恰好是哪里的东西翻倒。不管怎样的OOM看起来,当你分配你的BufferedReader正在发生的事情。缓冲区的大小实际上比内容(上述61字节串)的尺寸更大。您可以指定一个较小的缓冲区<一个href=\"http://developer.android.com/reference/java/io/BufferedReader.html#BufferedReader%28java.io.Reader,%20int%29\"相对=nofollow> 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.

这篇关于出在字节分配内存不足的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆