[英](Android) OutOfMemoryError thrown while trying to throw OutOfMemoryError
所以我正在創建一個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;
}
我不太確定究竟是什么導致錯誤。 我懷疑是一個無限循環,但我找不到它,所以我在這里問。 在拋出錯誤之前,它會不斷分配內存。
這是控制台日志(如果需要): PasteBin
附加信息:
單擊按鈕時會運行statModa函數。
紅色意味着線。
我運行應用程序並獲得錯誤的值如下:
ArrayList statRed的值為{ 0.1,1,1,2,2,3,3,4,5 5 5}和
ArrayList uniqueRed的值為{ 0.1,1,2,3,4,5 }。
這些也不是給我這個錯誤的唯一值。 如果這個(maxFreq == statRed.size()/ uniqueRed.size())為真,則沒有錯誤,如果沒有,那么我得到錯誤。
PS我看到另一個問題有同樣的錯誤,只有一個答案是啟用largeHeap,但它對我不起作用。
OOm是因為行i = maxFreqIndex;
,對於給定的輸入, maxFreqIndex
是8並且不會改變,所以你得到無限循環for (int i=maxFreqIndex+1; i < statRed.size(); i++)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.