当BitmapFactory.nativeDecodeStream()耗尽android应用程序的内存时

忍者

总而言之,我在手机上遇到了内存不足的异常(4.4.4 MotoX)。拍照后,我从sdcard-path读取了照片,然后发生了异常。

12-18 11:12:47.294  11966-11966/com.geexfinance.geexfinance E/dalvikvm-heap﹕ Out of memory on a 42024976-byte allocation.
12-18 10:15:42.099  12905-12905/com.geexfinance.geexfinance E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.geexfinance.geexfinance, PID: 12905
    java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
            at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:620)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:634)
            at com.geexfinance.geexfinance.model.GeexCache.getBitmap(GeexCache.java:1825)
            at com.geexfinance.geexfinance.model.GeexCache.getBitmap(GeexCache.java:1940)
            at com.geexfinance.geexfinance.ui.GeexApplyInstallmentFragment.onActivityResult(GeexApplyInstallmentFragment.java:532)
            at android.app.Activity.dispatchActivityResult(Activity.java:5450)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3428)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3475)
            at android.app.ActivityThread.access$1300(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

看起来很奇怪,我在Huawei-pad(4.4.3)上运行相同的应用程序,从来没有OOM发生。

我看着ANDROID_STUDIO的内存监视器。在我的手机上,从32MB分配到90MB的内存,而当内存大约为48MB时,总是会发生OOM异常。在我的平板电脑上,从32MB分配给40MB的内存永远不会达到OOM。

我有一些问题:

  • 我的手机有2048MB的内存,为什么只有42MB的分配会导致OOM?
  • 为什么当内存为48MB时会发生OOM,这不是内存高峰。
  • 相同的应用程序,相同的用户行为(拍摄3张照片),为什么手机有例外而键盘却没有?
尼古拉·德斯波托斯基(Nikola Despotoski)
my phone have 2048MB memory ,why only a 42MB allocation leads a OOM?

设备的总RAM内存与每个应用程序实例分配的堆大小无关。

why OOM occurs when memory is 48MB,it's not a the memory peak.

如上所述,当请求的内存超过堆大小时,就会发生OOM。

same application, same user behavior(take 3 photos), why phone have a exception and pad not?

与无法复制OOM相关的因素很多,可能是照片的详细信息和大小,也可能是该时间点该应用程序的当前内存消耗。阅读有关如何有效处理大型Bitmaps的文章,我相信问题将消失。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

BitmapFactory:无法解码流:java.io.FileNotFoundException:打开失败:EACCES(拒绝)对Android Q的

BitmapFactory无法解码流

BitmapFactory.decodeResource在Android 2.2中返回可变的位图,在Android 1.6中返回不可变的位图

Android BitmapFactory.decodeByteArray获取比iOS UIImage交换的照片宽度/高度

Android BitmapFactory.decodeResource占用过多内存

Android 5.0不支持BitmapFactory.Options inPurgeable

来自Assets的BitmapFactory.decodeStream在Android 7上返回null

在Android Nougat上拍摄照片后,BitmapFactory无法从Uri解码位图

Kotlin BitmapFactory IllegalStateException

ImageDecoder与BitmapFactory

Android BitmapFactory.decodeFile(path)始终返回null

使用GZIP文件上的BitmapFactory.DecodeStreamAsync将URL流传输到Android.Graphics.Bitmap

Android Studio Kotlin:BitmapFactory.decodeStream()返回null

为什么Android BitmapFactory.decodeFile()需要4倍的内存?

Android中的BitmapFactory.decodeFile和ImageView奇怪的东西

BitmapFactory.decodeFile的内存不足并带有图像2400x2400

BitmapFactory.decodeResource在Android 2.2上为null

BitmapFactory和HTTPUrlConnection问题

如果多次使用,则Android BitmapFactory.decodeResource内存不足

Android:BitmapFactory解码速度缓慢

在Android中,BitmapFactory.decodeByteArray返回带有inJustDecoteBounds = true的null

当应用程序因内存耗尽而崩溃时,我想显示消息框

在android.graphics.BitmapFactory.nativeDecodeAsset(本地方法)

来自Assets的BitmapFactory.decodeStream有时在Android 7上失败

Android无法解码流:bitmapFactory.decodeFile()的java.io.FileNotFoundException

Android BitmapFactory与BufferedInputStream

如何使用 Android BitmapFactory.Options.inBitmap?

BitmapFactory.decodeFile() 返回 null xamarin.android

使用 BitmapFactory 在画布上 onDraw 时出错