Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Android) OutOfMemoryError thrown while trying to throw OutOfMemoryError

Tags:

java

android

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:

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

Additional info:

The function statModa is ran when a button is clicked.

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 uniqueRed has the values {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.

P.S. 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.

like image 673
Alexander Dimitrov Avatar asked Dec 15 '15 19:12

Alexander Dimitrov


1 Answers

The OOm is because of the line 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++)

like image 149
igorepst Avatar answered Nov 10 '22 06:11

igorepst