Android 无法分配OutOfMemoryError

Android 无法分配OutOfMemoryError,android,out-of-memory,Android,Out Of Memory,由于内存问题,我有一些崩溃。我得到的日志大致如下: Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 557942800 byte allocation with 25165824 free bytes and 374MB until OOM, target footprint 169310272, growth limit 536870912 at com.appacoustic.rt.domain

由于内存问题,我有一些崩溃。我得到的日志大致如下:

Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 557942800 byte allocation with 25165824 free bytes and 374MB until OOM, target footprint 169310272, growth limit 536870912
       at com.appacoustic.rt.domain.calculator.processing.ToDoubleSamplesKt.toDoubleSamples(ToDoubleSamplesKt.java:25)
       at com.appacoustic.rt.domain.calculator.ReverbTimeCalculator.invoke(ReverbTimeCalculator.java:25)
       at com.appacoustic.rt.framework.audio.recorder.Recorder.stop(Recorder.java:25)
       at com.appacoustic.rt.framework.audio.recorder.Recorder$stop$1.invokeSuspend(Recorder.java:12)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:2)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:2)
       at kotlinx.coroutines.EventLoop.processUnconfinedEvent(EventLoop.java:2)
       at kotlinx.coroutines.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuationKt.java:120)
       at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(CancellableKt.java:9)
       at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.java:9)
       at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.java:9)
       at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(BuildersKt__Builders_commonKt.java:4)
       at kotlinx.coroutines.BuildersKt.launch(BuildersKt.java:4)
       at kotlinx.coroutines.BuildersKt.launch$default(BuildersKt.java:4)
       at com.appacoustic.rt.presentation.measure.MeasureViewModel$handleStartClicked$1$1.onFinish(MeasureViewModel.java:23)
       at com.appacoustic.rt.domain.ButtonStateHandler$start$1.onFinish(ButtonStateHandler.java:4)
       at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7397)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)

我在这里做的是音频处理操作

例如:

fun ByteArray.toDoubleSamples(): DoubleArray = mapPairsToDoubles { a, b ->
    (a.toInt() and 0xFF or (b.toInt() shl 8)).toDouble()
}

inline fun ByteArray.mapPairsToDoubles(block: (Byte, Byte) -> Double) =
    DoubleArray(size / 2) { i ->
        block(
            this[2 * i],
            this[2 * i + 1]
        )
    }
fun DoubleArray.schroederIntegral(): DoubleArray {
    val out = DoubleArray(size)
    for (index in indices) {
        out[index] = this[index] * this[index]
    }

    var aux = out[0]
    for (index in 1 until size) {
        aux += out[index]
        out[index] = aux
    }

    return out
}
或者这个:

fun ByteArray.toDoubleSamples(): DoubleArray = mapPairsToDoubles { a, b ->
    (a.toInt() and 0xFF or (b.toInt() shl 8)).toDouble()
}

inline fun ByteArray.mapPairsToDoubles(block: (Byte, Byte) -> Double) =
    DoubleArray(size / 2) { i ->
        block(
            this[2 * i],
            this[2 * i + 1]
        )
    }
fun DoubleArray.schroederIntegral(): DoubleArray {
    val out = DoubleArray(size)
    for (index in indices) {
        out[index] = this[index] * this[index]
    }

    var aux = out[0]
    for (index in 1 until size) {
        aux += out[index]
        out[index] = aux
    }

    return out
}
我不太确定这里出了什么问题。我想这与所花费的足够内存有关,但我使用的44100 Hz信号最多为1秒

当然这是一件愚蠢的事情,可能与双打浪费太多空间或类似的事情有关。但我不知道如何修复它

顺便说一句,车祸有点随机。这种情况并不总是发生

如果你想深入研究,回购协议就在这里:


有没有办法解决这个问题?:-)

这是一个愚蠢的错误。由于另一个地方的州问题,我打了两次录音。开始录制


顺便说一句,我得到了一个
-38
错误。实际上,我开始录制,在发生内存错误时它停止了。

发布OOM的完整堆栈跟踪。在第二种情况下,您尝试分配一个557942800字节=532MB的连续内存块。有些安卓设备的整个系统RAM小于这个值。“有没有办法修复它?”--堆栈跟踪应该准确地告诉您内存分配的来源。完成,@m0skit0.Debug,并查看您传递给DoubleSamples()的字节数组有多大。。我添加了一些日志,以便在@m0skit0中查看它。大小正常:字节数组约为0.15 MB,转换后的双数组为0。60 MB。