如何修复Android中关于数组数组的内存泄漏?

如何修复Android中关于数组数组的内存泄漏?,android,arrays,memory-leaks,bitmap,Android,Arrays,Memory Leaks,Bitmap,我已经创建了一个应用程序,它获取Android设备捕获的图像,并在ImageView中显示该图像。然后,用户可以按下按钮对图像进行模糊或去模糊。当我在我的安卓设备上运行应用程序时,我可以用相机拍摄图像并显示出来,而不会出现任何问题。当我按下“模糊”按钮时出现问题,该按钮运行一些代码来模糊图像。应用程序被冻结,我得到一行代码的OutOfMemoryException,该代码创建了一个新数组,并将其存储在嵌套for循环中的另一个数组中 这是嵌套for循环的代码: for (int y = 0; y

我已经创建了一个应用程序,它获取Android设备捕获的图像,并在ImageView中显示该图像。然后,用户可以按下按钮对图像进行模糊或去模糊。当我在我的安卓设备上运行应用程序时,我可以用相机拍摄图像并显示出来,而不会出现任何问题。当我按下“模糊”按钮时出现问题,该按钮运行一些代码来模糊图像。应用程序被冻结,我得到一行代码的OutOfMemoryException,该代码创建了一个新数组,并将其存储在嵌套for循环中的另一个数组中

这是嵌套for循环的代码:

for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        int xTranslated = (x + width / 2) % width;
        int yTranslated = (y + height / 2) % height;

        double real = temp[2 * (xTranslated + yTranslated * width)];
        double imaginary = temp[2 * (xTranslated + yTranslated * width) + 1];

        degradation[2 * (x + y * width)] = real;
        degradation[2 * (x + y * width) + 1] = imaginary;

        Complex c = new Complex(real, imaginary);
        complex[y * width + x] = c;
    }
}
这里是关于android崩溃的

Pierre Yves在11:39时谈到OOM(OutOfMemory错误)。所以OOM中的问题不是OOM发生的地方。你应该分析你的应用程序,找出内存消耗最多的地方。我应该承认OOM是最难解决的错误之一


祝你好运

请发布更多代码。@Okas我现在添加了完整的运动模糊方法。请尝试计算您试图分配的内存量,例如高度和宽度值等。查看您的代码,很可能您分配的内存太多,实际上没有泄漏。@Okas循环看起来不一样分配太多。即使它们是重复创建的,它们也大多是基本体。最大的问题是泄漏到方法之外的某个复杂的地方。检查是否有构造函数未正确解除对对象的引用。因此临时数组的大小将为2*2000*2000*8字节(双精度大小),略小于64 MB。为什么您认为可以分配这么多?为了跟踪OOM,请执行堆转储并使用
public Complex[] motionBlur(double[] degradation, int width, int height, double alpha, double gamma, double sigma) {
    Complex[] complex = new Complex[width * height];

    double[] temp = new double[2 * width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            double teta = Math.PI * ( (((x - width/2) % width) * gamma) + ((((y - height/2) % height) * sigma) ));

            Sinc sinc = new Sinc();

            double real = (Math.cos(teta) * sinc.value(teta)) * alpha;
            double imaginary = (Math.sin(teta) * sinc.value(teta)) * alpha;

            Complex cConj = new Complex(real, imaginary).conjugate();

            temp[2 * (x + y * width)] = cConj.getReal();
            temp[2 * (x + y * width) + 1] = cConj.getImaginary();
        }
    }

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int xTranslated = (x + width / 2) % width;
            int yTranslated = (y + height / 2) % height;

            double real = temp[2 * (xTranslated + yTranslated * width)];
            double imaginary = temp[2 * (xTranslated + yTranslated * width) + 1];

            degradation[2 * (x + y * width)] = real;
            degradation[2 * (x + y * width) + 1] = imaginary;

            Complex c = new Complex(real, imaginary);
            complex[y * width + x] = c;
        }
    }

    return complex;
}
Complex c = new Complex(real, imaginary);