位图大小超过 Vm 预算错误 android [英] bitmap size exceeds Vm budget error android

查看:32
本文介绍了位图大小超过 Vm 预算错误 android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我切换到横向模式时,很少有对象是用全屏位图创建的.

When I change to landscape mode, few objects are created with bitmap of full screen.

当我滚动另一个对象被调用并显示它的位图时,当我重复执行此操作时,位图大小超过vm预算错误,我已经完成了诸如recycle()之类的所有事情,设置为null,然后调用GC(),还是出现同样的错误.正在创建位图....

When I scroll the other object is called and its bitmap is displayed, when I doing this repeatedly , bitmap size exceeds vm budget error, I have done all the things like recycle(), set null and then called GC(), still I have same error. creating bitmap....

    bitmap = Bitmap.createBitmap(ChartProperties.getChartWidth(), 
                    ChartProperties.getChartHeight(),
    Bitmap.Config.RGB_565);

    imageCache.put(String.valueOf(LandscapeChartActivity.getActiveFeature()),
                    new SoftReference(bitmap));

    if(imageCache != null){

        for (int i = 0; i < imageCache.size(); i++) {

            if (imageCache.get(String.valueOf(i)) != null) {
                imageCache.get(String.valueOf(i)).get().recycle();
                imageCache.put(String.valueOf(i), null);                    
            }

        }
        Runtime.getRuntime().gc();
        imageCache.clear();
        imageCache = null;

推荐答案

我也遇到了 OOME 因为位图的问题.

I also had the same problem OOME because of bitmaps.

当方向从 PORTRAIT 变为 LANDSCAPE 时,反之亦然,之前的 UI 将被完全丢弃,并加载并显示一个新的 UI,在这种情况下,如果您的应用程序中使用了许多位图,则需要在适当的地方.

When orientation changes from PORTRAIT to LANDSCAPE and vice-versa, the previous UI is completely discarded, and a new UI is loaded and displayed, In this case if you are using many bitmaps in your app, you need to release them at proper places.

要检查您设备的方向,请参阅:在 Android 手机上检查方向

To check the orientation of your device, please see this: Check orientation on Android phone

在您的情况下,您需要在方向更改期间清除位图.

In your case, you need to clear bitmaps during orientation change.

在上面的链接中,您可以找到如何获取当前方向.因此,在每次更改方向时,调用上面的代码来清理位图.

On above link you can found, how to get the current orientation. So on each orientation change, call your above code that cleans up the bitmaps.

现在,当我们检查 logcat 时,总会出现一个日志说 GC_,但我无法理解,所以我发现了一个关于内存泄漏问题的惊人文档:http://codelog.dexetra.com/getting-around-android-memory-blues

Now, when we check the logcat, there is always a log comes up saying GC_, but I could not understand that, so I found an amazing doc on memory leak issue: http://codelog.dexetra.com/getting-around-android-memory-blues

以上链接对您的问题非常有用.

The above link is very useful for your problem.

现在,当您的应用程序中存在内存泄漏时会发生 OOME,因此要检查这一点,请安装 MAT for eclipse.您可以在以下位置找到它:http://www.eclipse.org/mat/downloads.php

Now, the OOME occurs when there is memory leak in your app., so to check that, please install the MAT for eclipse. You can find it at: http://www.eclipse.org/mat/downloads.php

它是一个有点复杂的软件,但当你经历它时,你就会明白,它是非常有用的软件.

Its a bit complicated software but as you go through it, you will understand, its pretty useful software.

即使这不能解决您的问题,也请使用 WeakReference 来处理位图.

Even if this doesn't solves your problem, use the WeakReference for bitmaps.

请参考这个链接:如何在Java中使用WeakReference和 Android 开发?

如果我知道更多信息,我会更新这篇文章.

If I get know some more info, I will update this post.

如果您的问题得到解决,请更新您的帖子.

Please update your post, if you get solution to your problem.

谢谢:)

这篇关于位图大小超过 Vm 预算错误 android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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