Dealing with the “Bitmap Size Exceeds VM Budget” error

本文提供了解决在Android应用中频繁遇到的java.lang.OutOfMemoryError: BitmapSizeExceedsVMBudget错误的方法。通过在布局文件中设置父视图的ID属性,并在Activity的onDestroy()方法中调用unbindDrawables()方法释放内存,可以有效解决这一问题。同时,文章还提到了另一种解决方案,即在manifest文件中配置Activity响应屏幕旋转,避免Activity销毁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dealing with the “Bitmap Size Exceeds VM Budget” error

One of the most common errors that I found developing Android Apps is the “java.lang.OutOfMemoryError: Bitmap Size Exceeds VM Budget” error. I found this error frecuently on activities using lots of bitmaps after changing orientation: the Activity is destroyed, created again and the layouts are “inflated” from the XML consuming the VM memory avaiable for bitmaps.

Bitmaps on the previous activity layout are not properly deallocated by the garbage collector because they have crossed references to their activity. After many experiments I found a quite good solution for this problem.

First, set the “id” attribute on the parent view of your XML layout:

1
2
3
4
5
6
7
<? xml version = "1.0" encoding = "utf-8" ?>
  android:layout_width = "fill_parent"
  android:layout_height = "fill_parent"
  android:id = "@+id/RootView"
>
...

Then, on the onDestroy()  method of your Activity, call the unbindDrawables() method passing a refence to the parent View and then do a System.gc()

@Override
protectedvoid onDestroy() {
     super .onDestroy();
 
    unbindDrawables(findViewById(R.id.RootView));
    System.gc();
}
 
privatevoid unbindDrawables(View view) {
     if (view.getBackground() != null ) {
        view.getBackground().setCallback( null );
    }
     if (view instanceof ViewGroup) {
         for ( int i = 0 ; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
}

This unbindDrawables() method explores the view tree recursively and:

  • Removes callbacks on all the background drawables
  • Removes childs on every viewgroup

This solved the problem on many of our Mobialia apps.

UPDATE 2011-03-30:

Today @luiskap from SpartanBits told me another good solution: if you don’t need different layouts for portrait and landscape modes, you can make your activity react to orientation changes (avoiding activity destroy) adding to your activity’s manifest: android:configChanges="keyboardHidden|orientation"and overriding the onConfigurationChanged method, calling setContentView reusing the already created views. There is a good explanation on StackOverflow.

6 comments to Dealing with the “Bitmap Size Exceeds VM Budget” error

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值