简体   繁体   English

(Android)尝试抛出OutOfMemoryError时抛出OutOfMemoryError

[英](Android) OutOfMemoryError thrown while trying to throw OutOfMemoryError

So I'm creating an Android app and this code is what throws "Caused by: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available" error: 所以我正在创建一个Android应用程序,这个代码抛出“引起:java.lang.OutOfMemoryError:在尝试抛出OutOfMemoryError时抛出OutOfMemoryError;没有可用的堆栈跟踪”错误:

public ArrayList<Double> statModa(ArrayList<Double> statRed, ArrayList<Double> uniqueRed) {
    ArrayList<Double> result = new ArrayList<>();
    int maxFreqIndex = 0;
    int maxFreq = Collections.frequency(statRed, uniqueRed.get(0));
    for (int i=1; i < statRed.size(); i++){
        int currentFreq = Collections.frequency(statRed, statRed.get(i));
        if ((currentFreq >  maxFreq) || (currentFreq == maxFreq && statRed.get(i) == statRed.get(maxFreqIndex))) {
            maxFreqIndex = i;
            maxFreq = currentFreq;
        }
    }

    if (maxFreq == statRed.size() / uniqueRed.size())
        return result;

    if (maxFreqIndex == statRed.size()-1){
        result.add(statRed.get(maxFreqIndex));
        return result;
    }

    for (int i=maxFreqIndex+1; i < statRed.size(); i++){
        int currentFreq = Collections.frequency(statRed, statRed.get(i));
        if (currentFreq != maxFreq) {
            result.add(statRed.get(maxFreqIndex));
            int tempIndex = maxFreqIndexFinder(statRed, i, maxFreq);
            if (tempIndex == maxFreqIndex) {
                break;
            } else {
                i = tempIndex;
                maxFreqIndex = tempIndex;
            }
        } else {
            double temp = statRed.get(maxFreqIndex);
            int tempTimes = 1;
            for (int j=1; i + currentFreq*j - 1 < statRed.size(); j++){
                if (currentFreq == Collections.frequency(statRed, statRed.get(i + currentFreq*j - 1))) {
                    temp += statRed.get(i + currentFreq * j - 1);
                    tempTimes++;
                    maxFreqIndex = i + currentFreq * j - 1;
                } else {
                    break;
                }
            }
            result.add(temp/(double)tempTimes);
            i = maxFreqIndex;
        }
    }

    return result;
}

public int maxFreqIndexFinder(ArrayList<Double> statRed, int startingIndex, int maxFreq){
    int resultIndex = startingIndex - 1;
    int count = 0;
    double lastFreqValue = 0;
    for (int i = startingIndex; i < statRed.size(); i++){
        int currentFreq = Collections.frequency(statRed, statRed.get(i));
        if (currentFreq == maxFreq) {
            count++;
            if (count == 1) {
                resultIndex = i;
                lastFreqValue = statRed.get(i);
            } else {
                if (lastFreqValue == statRed.get(i)) {
                    resultIndex = i;
                    lastFreqValue = statRed.get(i);
                } else {
                    break;
                }
            }
        }
    }
    return resultIndex;
}

I'm not quite sure what exactly causes the error. 我不太确定究竟是什么导致错误。 I suspect a infinite loop, but I couldn't find it, so I'm asking here. 我怀疑是一个无限循环,但我找不到它,所以我在这里问。 Also before throwing the error it continuously allocates memory. 在抛出错误之前,它会不断分配内存。

Here's the console log (if needed): PasteBin 这是控制台日志(如果需要): PasteBin

Additional info: 附加信息:

The function statModa is ran when a button is clicked. 单击按钮时会运行statModa函数。

Red means line. 红色意味着线。

The values I ran the app with and got an error are as follows: 我运行应用程序并获得错误的值如下:

ArrayList statRed has the values {0.1, 1, 1, 2, 2, 3, 3, 4, 5 5 5} and ArrayList statRed的值为{ 0.1,1,1,2,2,3,3,4,5 5 5}和

ArrayList uniqueRed has the values {0.1, 1, 2, 3, 4, 5}. ArrayList uniqueRed的值为{ 0.1,1,2,3,4,5 }。

Also these are not the only values that give me this error. 这些也不是给我这个错误的唯一值。 If this (maxFreq == statRed.size() / uniqueRed.size()) is true then there is no error, if not then I get the error. 如果这个(maxFreq == statRed.size()/ uniqueRed.size())为真,则没有错误,如果没有,那么我得到错误。

PS I saw another question with the same error, there was only one Answer which was to enable largeHeap, but it didn't work for me. PS我看到另一个问题有同样的错误,只有一个答案是启用largeHeap,但它对我不起作用。

The OOm is because of the line i = maxFreqIndex; OOm是因为行i = maxFreqIndex; , as for the given input maxFreqIndex is 8 and doesn't change, so you get infinite loop at for (int i=maxFreqIndex+1; i < statRed.size(); i++) ,对于给定的输入, maxFreqIndex是8并且不会改变,所以你得到无限循环for (int i=maxFreqIndex+1; i < statRed.size(); i++)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM