Failed to allocate a 27394783 byte allocation with 15063472 free bytes and 14MB until OOM exception

huangapple 未分类评论50阅读模式
英文:

Failed to allocate a 27394783 byte allocation with 15063472 free bytes and 14MB until OOM exception

问题

# 场景 #
我正在开发一个Android应用程序,该应用程序会下载加密的视频文件,将其保存在内存中,然后在用户点击Android应用程序中的播放按钮时对其进行解密。解密完成后播放该视频。

# 问题 #
在大多数设备上,应用程序运行良好。但是在红米Note 4上(根据我在3台不同的红米Note 4上的测试),它会抛出异常:

    抛出OutOfMemoryError "无法分配27394783字节的内存,其中有15063472字节的可用内存和14MB的剩余内存,直到OOM"

# Android Manifest.xml #
        android:name=".receiver.ConnectivityListener"
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyMaterialTheme"
        android:usesCleartextTraffic="true"

# 在 build.gradle 中 #
    dexOptions {
        incremental true
        javaMaxHeapSize "4g"
        preDexLibraries true
        dexInProcess = true
    }

# 日志输出 #
    2020-03-21 15:23:40.733 19593-19699/com.ehiddenbrain.prod I/art: 强制收集26MB分配的软引用
    2020-03-21 15:23:40.733 19593-19699/com.ehiddenbrain.prod I/art: 启动阻塞式GC分配
    2020-03-21 15:23:40.767 19593-19699/com.ehiddenbrain.prod I/art: 将目标GC堆从513MB调整到512MB
    2020-03-21 15:23:40.767 19593-19699/com.ehiddenbrain.prod I/art: 分配并发标记清除GC已释放3个(72B)AllocSpace对象,0个(0B)LOS对象,剩余2%,497MB / 512MB,暂停435微秒,总计33.194毫秒
    2020-03-21 15:23:40.767 19593-19699/com.ehiddenbrain.prod W/art: 抛出OutOfMemoryError "无法分配27394783字节的内存,其中有15063472字节的可用内存和14MB的剩余内存,直到OOM"

    2020-03-21 15:23:40.778 19593-19699/com.ehiddenbrain.prod E/AndroidRuntime: 致命异常: AsyncTask#9
    进程: com.ehiddenbrain.prod, PID: 19593
    java.lang.RuntimeException: 执行doInBackground()时发生错误
        at android.os.AsyncTask$3.done(AsyncTask.java:318)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:760)
     导致于: java.lang.OutOfMemoryError: 无法分配27394783字节的内存,其中有15063472字节的可用内存和14MB的剩余内存,直到OOM
        at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:1112)
        at javax.crypto.Cipher.doFinal(Cipher.java:2056)
        at com.example.ehiddenbrain_v03.utility.Decoder.decryptVideo(Decoder.java:56)
        at com.example.ehiddenbrain_v03.background.DecoderBackgroundTask.doInBackground(DecoderBackgroundTask.java:66)
        at com.example.ehiddenbrain_v03.background.DecoderBackgroundTask.doInBackground(DecoderBackgroundTask.java:26)
        at android.os.AsyncTask$2.call(AsyncTask.java:304)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
        at java.lang.Thread.run(Thread.java:760)
    2020-03-21 15:23:40.779 19593-19699/com.ehiddenbrain.prod W/AndroidRuntime: 完成raiseRlimit,rlim_cur:4096  rlim_max:4096
    2020-03-21 15:23:40.945 19593-19696/com.ehiddenbrain.prod V/FA: 记录用户参与度,毫秒数: 4696
    2020-03-21 15:23:40.947 19593-19696/com.ehiddenbrain.prod V/FA: 活动已暂停,时间: 240603587

如果有人了解此问题,请帮助我...
谢谢!!!
英文:

Scenario

I am working on android application which downloads encrypted video file, save it in memory and then decrypt it when user click on play button of that file from the android application. And after decryption play that video.

#Problem#
App works fine in most of the devices. But in Redmi note 4 (as per my testing on 3 different redmi note 4)it gives exception

Throwing OutOfMemoryError "Failed to allocate a 27394783 byte allocation with 15063472 free bytes and 14MB until OOM

#Android Menifest.xml#

    android:name=".receiver.ConnectivityListener"
    android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyMaterialTheme"
    android:usesCleartextTraffic="true"

#In build.gradle#

dexOptions {
    incremental true
    javaMaxHeapSize "4g"
    preDexLibraries true
    dexInProcess = true
}

#logcat#

2020-03-21 15:23:40.733 19593-19699/com.ehiddenbrain.prod I/art: Forcing collection of SoftReferences for 26MB allocation
2020-03-21 15:23:40.733 19593-19699/com.ehiddenbrain.prod I/art: Starting a blocking GC Alloc
2020-03-21 15:23:40.767 19593-19699/com.ehiddenbrain.prod I/art: Clamp target GC heap from 513MB to 512MB
2020-03-21 15:23:40.767 19593-19699/com.ehiddenbrain.prod I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 2% free, 497MB/512MB, paused 435us total 33.194ms
2020-03-21 15:23:40.767 19593-19699/com.ehiddenbrain.prod W/art: Throwing OutOfMemoryError "Failed to allocate a 27394783 byte allocation with 15063472 free bytes and 14MB until OOM"


2020-03-21 15:23:40.778 19593-19699/com.ehiddenbrain.prod E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #9
Process: com.ehiddenbrain.prod, PID: 19593
java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:318)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:760)
 Caused by: java.lang.OutOfMemoryError: Failed to allocate a 27394783 byte allocation with 15063472 free bytes and 14MB until OOM
    at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:1112)
    at javax.crypto.Cipher.doFinal(Cipher.java:2056)
    at com.example.ehiddenbrain_v03.utility.Decoder.decryptVideo(Decoder.java:56)
    at com.example.ehiddenbrain_v03.background.DecoderBackgroundTask.doInBackground(DecoderBackgroundTask.java:66)
    at com.example.ehiddenbrain_v03.background.DecoderBackgroundTask.doInBackground(DecoderBackgroundTask.java:26)
    at android.os.AsyncTask$2.call(AsyncTask.java:304)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
    at java.lang.Thread.run(Thread.java:760) 
2020-03-21 15:23:40.779 19593-19699/com.ehiddenbrain.prod W/AndroidRuntime: finished raiseRlimit, rlim_cur:4096  rlim_max:4096
2020-03-21 15:23:40.945 19593-19696/com.ehiddenbrain.prod V/FA: Recording user engagement, ms: 4696
2020-03-21 15:23:40.947 19593-19696/com.ehiddenbrain.prod V/FA: Activity paused, time: 240603587

If anyone know about this issue please help me ...
Thanks !!!

答案1

得分: 0

在我的解决方案中,我将 ByteArrayOutputStream(内存)更改为 FileOutputStream(磁盘)。它工作得很好!

val fis = ByteArrayInputStream(fileData)
val cin = CipherInputStream(fis, cipher)
val fos = FileOutputStream(fileToSave)

val b = ByteArray(128)
var numberOfBytesRead: Int
while (cin.read(b).also { numberOfBytesRead = it } >= 0) {
    fos.write(b, 0, numberOfBytesRead)
}
英文:

In my solution I change ByteArrayOutputStream (memory) by FileOutputStrem (disk). It works fine!

val fis = ByteArrayInputStream(fileData)
val cin = CipherInputStream(fis, cipher)
val fos = FileOutputStream(fileToSave)

val b = ByteArray(128)
var numberOfBytesRead: Int
while (cin.read(b).also { numberOfBytesRead = it } >= 0) {
    fos.write(b, 0, numberOfBytesRead)
}

huangapple
  • 本文由 发表于 2020年4月6日 13:11:24
  • 转载请务必保留本文链接:https://java.coder-hub.com/61053299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定