gpt4 book ai didi

android - AbstractStringBuilder enlargeBuffer 中的 OutOfMemoryError

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:28:39 26 4
gpt4 key购买 nike

我正在阅读 Stream 中的 youtube 响应并将其转换为字符串。使用以下代码:

网址:http://gdata.youtube.com/feeds/api/users/cnn/uploads?&v=2&max-results=25&alt=json

private String convertStreamToString(InputStream is) {

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}

此代码在除 Android 4.0 设备外的所有设备上都可以正常工作。我收到以下 logcat:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
at java.lang.StringBuilder.append(StringBuilder.java:216)
at com.android.mypack.MyApplications.convertStreamToString(MyApplications.java:959)
at com.android.mypack.MyApplications.access$61(MyApplications.java:951)
at com.android.mypack.MyApplications$YoutubeTask.doInBackground(MyApplications.java:2303)
at com.android.mypack.MyApplications$YoutubeTask.doInBackground(MyApplications.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more
java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
at java.lang.StringBuilder.append(StringBuilder.java:216)
at com.android.mypack.MyApplications.convertStreamToString(MyApplications.java:959)
at com.android.mypack.MyApplications.access$61(MyApplications.java:951)
at com.android.mypack.MyApplications$YoutubeTask.doInBackground(MyApplications.java:2303)
at com.android.mypack.MyApplications$YoutubeTask.doInBackground(MyApplications.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)

请帮助我克服这个错误。提前致谢。

最佳答案

嗯,当虚拟机内存不足时,OutOfMemoryErrors 就很明显了。这里的重点是确切的堆栈跟踪可能无法提供信息,因为另一个线程可能会使用所有内存。或者在这段代码之前这个线程发生了一些耗内存的事情,而这段代码只是“最后一根稻草”。这个网址肯定不会包含太多字节,我认为问题出在另一个地方。

你可以试试内存分析器: Android ==> Memory Analysing ==> Eclipse memory analyzer?

关于android - AbstractStringBuilder enlargeBuffer 中的 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13928557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com