简体   繁体   English

内存不足错误:StringBuilder.append 抛出 OutOfMemoryError

[英]Out of memory error : StringBuilder.append throwing OutOfMemoryError

I'm working on an application that fetches data from a bluetooth device.我正在开发一个从蓝牙设备获取数据的应用程序。 But sometimes it throws OOM error while performing some operation但有时它在执行某些操作时会抛出OOM 错误

Here is the code这是代码

The Following way I am storing the data in an ArrayList<String>以下方式我将数据存储在ArrayList<String>

private ArrayList<String> dataList; 
if (response.compareTo("Some Filter") != 0 //response is of String type
{
    dataList.add(response);
}

And in the below for each loop it throws OOM error在下面for each循环中,它会引发 OOM 错误

for (String s : dataList) { 
if(s.length()>8)
    dataList.set(dataList.indexOf(s), s.substring(8));  
else
    dataList.set(dataList.indexOf(s), "");
}

String downloadedData = "";
for (String s : dataList) {
    downloadedData += s; //This one is the 280th line throwing OOM
}

So far I have read this post but it gives solutions for reading data in json or web response到目前为止,我已经阅读了这篇文章,但它提供了在 json 或 web 响应中读取数据的解决方案

And i know OOM error can't be handled but prevented by following a good Architecture 而且我知道无法处理 OOM 错误,但可以通过遵循良好的架构来防止

And there are also these two Callbacks for Android Android 也有这两个回调

  1. onLowMemory() onLowMemory()
  2. onTrimMemory() onTrimMemory()

But i am not sure how to go for the solution !!但我不知道如何寻求解决方案!!

The crash Stacktrace is:崩溃堆栈跟踪是:

java.lang.OutOfMemoryError: Failed to allocate a 106990 byte allocation with 5840 free bytes and 5KB until OOM
 at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
 java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:146)
 at java.lang.StringBuilder.append(StringBuilder.java:216) 
 at Class_name.java:280.

Any help is appreciated :)任何帮助表示赞赏:)

First: if (response.compareTo("Some Filter") != 0 ) that is just weird, you can just use equals method.首先: if (response.compareTo("Some Filter") != 0 )这很奇怪,你可以使用equals方法。

Then you are adding response to the list, and then you are reducing length of all responses in a list if they are higher than 8 (also, you did substring(8) , that trims the first 8 letters and I think you wanted to preserve first 8 letters and delete the rest, if yes then do this substring(0, 8) ).然后你将响应添加到列表中,然后你减少列表中所有响应的长度,如果它们大于 8(另外,你做了substring(8) ,它修剪了前 8 个字母,我认为你想保留前 8 个字母并删除其余字母,如果是,则执行此substring(0, 8) )。

My answer is: why not just take care of the things you are doing in a for-each when you are adding it to a list?我的回答是:当你将它添加到列表时,为什么不只是照顾你在for-each中所做的事情? It would be a lot easier and it would be better performance-wise, and would probably fix your error.这会容易得多,而且在性能方面会更好,并且可能会修复您的错误。

Also, I'm not sure if you are initializing your list, maybe you just forgot to paste it (all you have is private ArrayList<String> dataList; ), but that's how you make a new list:另外,我不确定您是否正在初始化您的列表,也许您只是忘记粘贴它(您所拥有的只是private ArrayList<String> dataList; ),但这就是您创建新列表的方式:

List<String> list = new ArrayList<String>();

First your loop should use首先你的循环应该使用

for (int i = 0; i < dataList.size(); ++i) {
    String s = dataList.get(i);
    dataList.set(i, s.length() > 8 ? s.substring(8) : "");  
}

And the error points in another direction.错误指向另一个方向。

Try it is working试试看是否有效

 ArrayList<String> dataList = new ArrayList<>();
 String response = "CVMKVLC";
    if (response.compareTo("Some Filter") != 0 ) {
         //response is of String type     
         dataList.add(response);
    }

    for (String s : dataList) { //This line throws OOM
       if(s.length()>8)
           dataList.set(dataList.indexOf(s), s.substring(8));
       else
           dataList.set(dataList.indexOf(s), "");
       }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM