一个Bitmap引起的OOM问题的解决

1,出错日志:

08-14 10:04:00.858  1827  1852 E OpenGLRenderer: GL error:  Out of memory!
08-14 10:04:00.858  1827  1852 F OpenGLRenderer: GL errors! frameworks/base/libs/hwui/renderthread/CanvasContext.cpp:550
--------- beginning of crash
08-14 10:04:00.859  1827  1852 F libc    : Fatal signal 6 (SIGABRT), code -6 in tid 1852 (RenderThread)
08-14 10:04:00.860   167   167 W         : debuggerd: handling request: pid=1827 uid=10185 gid=10185 tid=1852
08-14 10:04:00.940  2054  2054 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
08-14 10:04:00.940  2054  2054 F DEBUG   : Build fingerprint: 'rockchip/rk3328_box/rk3328_box:7.1.2/NHG47K/root11222334:userdebug/test-keys'
08-14 10:04:00.941  2054  2054 F DEBUG   : Revision: '0'
08-14 10:04:00.941  2054  2054 F DEBUG   : ABI: 'arm'
08-14 10:04:00.941  2054  2054 F DEBUG   : pid: 1827, tid: 1852, name: RenderThread  >>> com.orbbec.ai.face <<<
08-14 10:04:00.941  2054  2054 F DEBUG   : signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
08-14 10:04:00.944  2054  2054 F DEBUG   : Abort message: 'GL errors! frameworks/base/libs/hwui/renderthread/CanvasContext.cpp:550'
08-14 10:04:00.944  2054  2054 F DEBUG   :     r0 00000000  r1 0000073c  r2 00000006  r3 00000008
08-14 10:04:00.944  2054  2054 F DEBUG   :     r4 d1d94978  r5 00000006  r6 d1d94920  r7 0000010c
08-14 10:04:00.944  2054  2054 F DEBUG   :     r8 d1bc2ac0  r9 00000000  sl eb56153c  fp eb561538
08-14 10:04:00.944  2054  2054 F DEBUG   :     ip 00000016  sp d1d93f38  lr ed34b1c7  pc ed34da24  cpsr 20070010
08-14 10:04:00.949  2054  2054 F DEBUG   : 
08-14 10:04:00.949  2054  2054 F DEBUG   : backtrace:
08-14 10:04:00.950  2054  2054 F DEBUG   :     #00 pc 00049a24  /system/lib/libc.so (tgkill+12)
08-14 10:04:00.950  2054  2054 F DEBUG   :     #01 pc 000471c3  /system/lib/libc.so (pthread_kill+34)
08-14 10:04:00.950  2054  2054 F DEBUG   :     #02 pc 0001d475  /system/lib/libc.so (raise+10)
08-14 10:04:00.950  2054  2054 F DEBUG   :     #03 pc 00018fc1  /system/lib/libc.so (__libc_android_abort+34)
08-14 10:04:00.950  2054  2054 F DEBUG   :     #04 pc 00017024  /system/lib/libc.so (abort+4)
08-14 10:04:00.950  2054  2054 F DEBUG   :     #05 pc 0000c3c9  /system/lib/libcutils.so (__android_log_assert+112)
08-14 10:04:00.950  2054  2054 F DEBUG   :     #06 pc 000240d7  /system/lib/libhwui.so
08-14 10:04:00.950  2054  2054 F DEBUG   :     #07 pc 00025ad3  /system/lib/libhwui.so
08-14 10:04:00.950  2054  2054 F DEBUG   :     #08 pc 00029095  /system/lib/libhwui.so (_ZN7android10uirenderer12renderthread12RenderThread10threadLoopEv+80)
08-14 10:04:00.950  2054  2054 F DEBUG   :     #09 pc 0000e345  /system/lib/libutils.so (_ZN7android6Thread11_threadLoopEPv+140)
08-14 10:04:00.950  2054  2054 F DEBUG   :     #10 pc 000665f5  /system/lib/libandroid_runtime.so (_ZN7android14AndroidRuntime15javaThreadShellEPv+80)
08-14 10:04:00.950  2054  2054 F DEBUG   :     #11 pc 00046c93  /system/lib/libc.so (_ZL15__pthread_startPv+22)
08-14 10:04:00.950  2054  2054 F DEBUG   :     #12 pc 00019a0d  /system/lib/libc.so (__start_thread+6)
08-14 10:04:01.019  1347  1358 W art     : Suspending all threads took: 6.954ms
08-14 10:04:01.407  2054  2054 E         : debuggerd: failed to kill process 1827: No such process
08-14 10:04:01.410   167   167 W         : debuggerd: resuming target 1827
08-14 10:04:01.410   413   483 I BootReceiver: Copying /data/tombstones/tombstone_07 to DropBox (SYSTEM_TOMBSTONE)
08-14 10:04:01.413   413  2059 W ActivityManager:   Force finishing activity com.orbbec.ai.face/.RgbTrackActivity

日志中除了提示OOM,然后就是指出是OpenGL出问题了。

初看之下也不知道是那里的错误。既然OpenGL出问题了,说明应该和图像渲染等有关系。然后就开始关注与图像显示相关的代码。

场景:

       在我的app中场景是这样的,实时从摄像头读取RGB的帧,读取到原始nv21格式的数据之后,传入底层的软件包,底层软件包给我返回一个Bitmap,然后我通过ImageView将返回的Bitmap显示出来。

网上关于OOM问题的解决办法:

可以参考博客:https://blog.csdn.net/u012758088/article/details/70145656

里面列出了主要的方法:

1,使用BitmapFactory.Options,在创建bitmap,使用decodeStream时对图片采用底内存占用量的编码方式,或者对图片进行压缩。

2,及时回收bitmap的内存。据说这种方式在2.3.3之后就不需要手动回收了。有gc自己回收。

3,通过LruCache + sd的缓存方式等。

--由于我的图片是直接从底层jar包获取到的,所以不能使用BitmapFactory。因此这两种方式都不适用。

 

最后我通过如下方法解决这个问题:

1,在显示图像之前使用rgbBitmap.setConfig(Bitmap.Config.ARGB_4444);方法降低rgbBitmap的内存占用量。

2,降低Imageview的大小。

这么搞之后,可以解决问题。

有同事建议使用弱引用WeakReference的方式来保存bitmap,使得bitmap可以在第一时间释放,但是这个方法不凑效。

 

最后,我们发现我的报out of memory的地方是在OpenGLRenderer: GL error:  Out of memory!,怀疑原因可能是ImageView这边可能有问题。又由于是视频的实时显示,因此我们想着是不是可以把Imageview 替换成SurfaceView来搞。还在尝试中回头把试验结果给附上来。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值