Android应用程序崩溃了对内存重新开张 [英] Android app crash out of memory on relaunch

查看:171
本文介绍了Android应用程序崩溃了对内存重新开张的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有造成大的位图一个臭名昭著的OOM错误。但我已经设法解决大多数问题。剩下唯一的问题发生在我之后单击后退,关闭应用程序,然后启动应用程序。然后应用程序会崩溃给了我一个OOM(内存不足)错误。如果我点击这个家不会发生。

So I'm having an infamous oom error caused by large bitmaps. But I've managed to fix most of the issues. The only issue remaining happens when I click back and close the app and then launch the app right after. Then the app will crash giving me a oom (out of memory) error. This wont happen if I click home.

这是怎么回事?我的猜测是,GC尚未完成清理,现在我启动它,而旧数据仍然躺在附近。当然,这不是一个新的应用程序,以便旧的推出和新推出的那张相同的应用程序内存限制之下。

Why is this happening? My guess is that the GC hasn't finished clearing up and now I start it up while the old data is still lying around. And of course it isn't a new app so the old launch and the new launch goes under the same app memory limitation.

在这个问题上和可能的解决方案的任何投入将是巨大的。

Any inputs on this issue and possible solutions would be great.

我已经tryed:

在所有下载的位图我用:

On all download of bitmaps I've used:

BitmapFactory.Options op = new Options();
op.inPurgeable = true;
bmImg = BitmapFactory.decodeStream(is,null,op);

使图像在尺寸上更小的宽度x高度(以kb大小大约相同)。 < - 这解决了问题,所以我有一个后备解决方案,除非是有人在那里与一个超​​级的解决办法:)

Making the images smaller in dimensions width x height (the size in kb is approximately the same). <-- This solves the problem so I have a fall-back solution unless there is someone out there with a super solution :)

错误日志的摘录:

06-25 04:29:28.917: E/AndroidRuntime(8819): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-25 04:29:28.917: E/AndroidRuntime(8819):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
06-25 04:29:28.917: E/AndroidRuntime(8819):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
06-25 04:29:28.917: E/AndroidRuntime(8819):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
06-25 04:29:28.917: E/AndroidRuntime(8819):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:715)
06-25 04:29:28.917: E/AndroidRuntime(8819):     at android.content.res.Resources.loadDrawable(Resources.java:1713)
06-25 04:29:28.917: E/AndroidRuntime(8819):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
06-25 04:29:28.917: E/AndroidRuntime(8819):     at android.widget.ImageView.<init>(ImageView.java:122)
06-25 04:29:28.917: E/AndroidRuntime(8819):     at android.widget.ImageView.<init>(ImageView.java:112)
06-25 04:29:28.917: E/AndroidRuntime(8819):     ... 23 more

修改
因此,两件事情解决我这个问题。

Edit: So two things fix this issue for me.


  1. 清除我的数据库中的OnDestroy(我的主要活动的大图片设置为null)。

  2. 制作大型图象更小。

但是,这只是提出了一个相同的基本问题,如果的onDestroy()被调用时,为什么没有一个新的打开之前我的活动正常关闭?此外,我看到我的活动继续关闭后长期运行。难道这是涉及到这个问题?如何跟踪下来的原因呢?

But this just raises the same fundamental question, if onDestroy() is called, why isn't my activity closed properly before a new one is opened? Also I see my activity keep on running long after it is closed. Could this be related to the issue? How do I track down the cause of this?

EDIT2 :本culprint似乎是我LruCache。我使用不在的OnDestroy()清除一个静态lrucache。当应用程序重新启动所有图像的lrucache仍然是present这会导致问题。我还在想为什么这只是在重新启动的问题吗?应该不会,这也成为一个问题,当我回到我的主要活动就在我关闭它?

Edit2: The culprint seems to be my LruCache. I use a static lrucache which isn't cleared in ondestroy(). When the app is restarted all the images in the lrucache is still present which causes problems. I'm still wondering why this only is a problem on restart? Shouldn't this also be a problem when I return to my main activity just before I close it?

推荐答案

该culprint似乎是我LruCache。我使用不在的OnDestroy()清除一个静态lrucache。当应用程序重新启动所有图像的lrucache仍然是present这会导致问题。我还在想为什么这只是在重新启动的问题吗?应该不会,这也成为一个问题,当我回到我的主要活动就在我关闭它?

The culprint seems to be my LruCache. I use a static lrucache which isn't cleared in ondestroy(). When the app is restarted all the images in the lrucache is still present which causes problems. I'm still wondering why this only is a problem on restart? Shouldn't this also be a problem when I return to my main activity just before I close it?

不过这个问题我临时/永久解决办法是清洁的的OnDestroy()所有静态引用,而且使用更小尺寸的图像。这似乎解决了我所有的问题,我也没能找到任何内存泄漏。

Nevertheless my temp/permanent fix of this issue is to clean all static references in ondestroy(), but also to use images of smaller size. This seems to solve all my problems as I'm also not able to find any memory leaks.

这篇关于Android应用程序崩溃了对内存重新开张的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆