gpt4 book ai didi

java - 嵌套循环在 android 中导致大量垃圾收集?

转载 作者:行者123 更新时间:2023-11-29 08:53:21 26 4
gpt4 key购买 nike

我在android中有这段代码,它导致了大量的GC日志

    // When turning into frequency domain we'll need complex numbers:
byte audio[] = out.toByteArray(); //approx size 827392
int amountPossible = 200;
Complex[][] results = new Complex[amountPossible][];

// For all the chunks:
for (int times = 0; times < amountPossible; times++) {
Complex[] complex = new Complex[4096];
for (int i = 0; i < 4096; i++) {
// Put the time domain data into a complex number with imaginary
// part as 0:
complex[i] = new Complex(audio[(times * 4096) + i], 0);
}
// Perform FFT analysis on the chunk:
results[times] = FFT.fft(complex);
}

下面是一些日志

    D/dalvikvm(10602): GC_CONCURRENT freed 805K, 88% free 3911K/31075K, paused 2ms+5ms
D/dalvikvm(10602): GC_CONCURRENT freed 1796K, 88% free 3957K/31075K, paused 1ms+2ms
D/dalvikvm(10602): GC_CONCURRENT freed 1811K, 88% free 3970K/31075K, paused 1ms+3ms
D/dalvikvm(10602): GC_CONCURRENT freed 1711K, 87% free 4102K/31075K, paused 2ms+3ms
D/dalvikvm(10602): GC_CONCURRENT freed 1806K, 87% free 4138K/31075K, paused 1ms+3ms
D/dalvikvm(10602): GC_CONCURRENT freed 1755K, 87% free 4226K/31075K, paused 1ms+3ms
D/dalvikvm(10602): GC_CONCURRENT freed 1827K, 87% free 4242K/31075K, paused 1ms+3ms
D/dalvikvm(10602): GC_CONCURRENT freed 1732K, 87% free 4258K/31075K, paused 2ms+2ms
D/dalvikvm(10602): GC_CONCURRENT freed 1714K, 86% free 4387K/31075K, paused 1ms+3ms

提前致谢

最佳答案

您的问题是,在 Java 中,Complex 是堆分配的 Java 对象。我相信它是不可变的,因此必须为每次计算创建新的 Complex 实例。这使得处理复数的数学运算极其缓慢并分配大量内存。因此应尽可能避免。

纯 Java 中唯一的解决方案是不使用内置的 Complex 类型,而是直接对成对的 double 数组执行 FFT,每个数组对应实部和虚部。或者,可以使用包含实部和虚部的单个数组,尽管对 this question 的评论暗示第一种方法往往会提供更好的性能。缺少非堆分配对象是 JVM 的一个不幸限制。

要获得更高的性能,最好的方法是使用 native 库。一种选择是 FFTS ,它已获得 BSD 许可并带有 JNI 绑定(bind)。它使用运行时代码生成和 SIMD 指令来实现远高于任何纯 Java 库的性能。

关于java - 嵌套循环在 android 中导致大量垃圾收集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21595929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com