Zum ersten Mal heute habe ich einen OutOfMemory Error getroffen. Ich versuche, bewegliche Daten aus einigen Daten in eine ArrayList zu berechnen, und stürzte beim ersten .add() - Schritt ab. Das Verfahren wird unterArrayList.add() verursacht OutOfMemory Fehler?
public ArrayList<Long> getNdaySMA(List<HistoricalQuote> history, int range){
long sum =0;
long SMA = 0;
ArrayList<Long> SMAs = new ArrayList<Long>();
//realRange is made due to the differences in defining "range in calculation vs speech
//a 10 day range for day 9 is actually from prices of day0 to day9, inclusive
int realRange =range-1;
//First step, add in placeholder 0s for the days within the range that have no value
//so if 10 day range, we have 0-> 9
for (int i=0;i<i+realRange;i++){
SMAs.add(i,0L);
}
//Next, actually calculate the SMAs for i.e. day 10
for (int i =0;i<history.size();i++){
//should be k<10, 0......9 = 10 days
for(int k=i+realRange;k==i;k--){
//Sum first from k=i+range-1 , go down to i.
//This should give us a value of RANGE
sum +=history.get(k).getClose().longValue();
}
//after summing up, we add calculate SMA and add it to list of SMAs
SMA = sum/range;
//we add the corresponding SMA to index i+range, made up of values calculated from before it
//to excel
SMAs.add(i+realRange,SMA);
sum =0;
}
return SMAs;
}
gezeigt
Die stacktrace als
java.lang.OutOfMemoryError
at java.util.ArrayList.add(ArrayList.java:154)
at com.xu.investo.MethodDatabase.getNdaySMA(MethodDatabase.java:46)
folgt Wo Linie 46 bezieht sich auf
SMAs.add(i,0L);
Ist dieser Fehler auftritt aufgrund der Verwendung des langen Zahlenformat? Irgendwelche Vorschläge sind willkommen.
Es ist nicht das Problem des Hinzufügens lang Ihre Schleife ist Endlosschleife ist es nie endend überprüfen Sie sorgfältig Bedingung "i
Ihr Problem gelöst? –
Ja, ich habe die Lösung gefunden, es ist die zweite Antwort. –