如何解决Android应用程序内存不足错误

发布于 2024-11-29 13:27:54 字数 788 浏览 0 评论 0原文

我有一个自定义列表视图。我列出了大约 200 张图像。这些图像存储在我的可绘制对象中。我已将图像转换为位图,并将图像显示为位图。每次打开活动时,都会列出图像。像这样,当我调用该活动大约 10 次时,它崩溃了,然后错误是

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

我回收了位图,并通过使用 System.gs 函数,我得到了以下内容

Clamp target GC heap from 25.464MB to 24.000MB
GC freed 322 objects / 70664 bytes in 146ms
 Clamp target GC heap from 25.471MB to 24.000MB
 GC freed 2 objects / 48 bytes in 138ms
Forcing collection of SoftReferences for 10720-byte allocation
 Clamp target GC heap from 25.269MB to 24.000MB
 DEBUG/dalvikvm(15311): GC freed 5080 objects / 211168 bytes in 121ms
 ERROR/dalvikvm-heap(15311): Out of memory on a 10720-byte allocation

现在我终于得到了这个 Out of memory on a 10720-字节分配错误

现在如何解决该错误,请朋友们帮忙

i have a custom list view. in that i am listing out images of around 200. The images are been stored in my drawable. I have converted the image to be a bitmap and i am displaying the image as bitmap. Every time when the activity is being opened the images get listed out. Like this when i am calling the activity for around 10 times it gets crashed and the error is

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

then i recycled the bitmaps and by using System.gs function i got the following

Clamp target GC heap from 25.464MB to 24.000MB
GC freed 322 objects / 70664 bytes in 146ms
 Clamp target GC heap from 25.471MB to 24.000MB
 GC freed 2 objects / 48 bytes in 138ms
Forcing collection of SoftReferences for 10720-byte allocation
 Clamp target GC heap from 25.269MB to 24.000MB
 DEBUG/dalvikvm(15311): GC freed 5080 objects / 211168 bytes in 121ms
 ERROR/dalvikvm-heap(15311): Out of memory on a 10720-byte allocation

Now atlast i got this Out of memory on a 10720-byte allocation error

Now how to resolve the error, please help me friends

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我爱人 2024-12-06 13:27:54

让我们检查一下您的情况的清单:

  1. 是否每次都会创建一项新活动,而您却忘记完成前一项活动?如果这样做,请确保通过将内存转储到 .hprof 文件中并使用 MemoryAnalyzer (http://www.eclipse.org/mat/) 来分析应用程序。检查内存中活动实例的数量,以确保它确实被清理。
  2. 如果您要重新加载相同的数据,您可能希望保留此活动并在启动它时使用 FLAG_ACTIVITY_REORDER_TO_FRONT,这样就不需要重新创建
  3. 您的可绘制对象有多大?在将其设置到 ImageView 之前,您可能需要调整它的大小

让我想出一些可能有用的内容并稍后添加

Let's run through the checklist for your situation:

  1. Is a new activity created every time and you forget to finish out the previous one? If you did, make sure that you profile the app by dumping the memory in .hprof file and use MemoryAnalyzer (http://www.eclipse.org/mat/). Check the number of instances of activity that you have in memory to make sure that it is actually get cleaned up.
  2. If you are reloading the same data, you might want to keep this activity around and use FLAG_ACTIVITY_REORDER_TO_FRONT when starting it so it doesn't need to recreate
  3. How big is your drawable? You might want to resize it before you set it to your ImageView

Let me think up some more that might be helpful and add later

ヅ她的身影、若隐若现 2024-12-06 13:27:54

您是否尝试过将位图变量设置为软引用?

Have you tried setting the Bitmap variables as Soft-references?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文