系统相关
首页 > 系统相关> > java-为什么出现内存不足错误?

java-为什么出现内存不足错误?

作者:互联网

我不断收到此错误?

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

解决方法:

您可能正在OOM`cos中加载该文本文件,该文件应很大,该文件在该StringBuilder的内存中.您应该将此文件而不是StringBuilder写入本地文件,以避免发生错误.

标签:out-of-memory,java,android
来源: https://codeday.me/bug/20191208/2089134.html