为什么我会出现内存不足错误?

时间:2011-07-25 17:11:05

标签: java android out-of-memory

我一直收到这个错误?

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)

每次我尝试运行此方法时:

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

所有这些方法都是从网站上托管的文本文件中检索URL。

编辑:调试指向我的代码行。当我跑步时,我没有记忆错误。

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

2 个答案:

答案 0 :(得分:3)

你可能会得到OOM,因为你要在StringBuilder的内存中加载那个应该很大的文本文件。您应该将此文件写入本地文件而不是StringBuilder以避免错误。

答案 1 :(得分:0)

如果您的应用程序中没有泄漏,那么这可能与Android HttpClient OOM on 4G/LTE (HTC Thunderbolt)

中讨论(并修复!)的问题相同