今天我第一次遇到OutOfMemory错误。我试图将一些数据中的移动平均值计算到ArrayList中,并在第一个.add()步骤中发生崩溃。方法如下所示
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;
}
堆栈跟踪如下
java.lang.OutOfMemoryError
at java.util.ArrayList.add(ArrayList.java:154)
at com.xu.investo.MethodDatabase.getNdaySMA(MethodDatabase.java:46)
第46行指的是
SMAs.add(i,0L);
由于使用了长数字格式,是否发生此错误?欢迎任何建议。
答案 0 :(得分:0)
我想我已经发现了问题。
我可能在这一行创建了一个无限循环
for (int i=0;i<i+realRange;i++){
SMAs.add(i,0L);
}
答案 1 :(得分:0)
看起来像无限循环:
for (int i=0;i<i+realRange;i++)
i
总是小于i+realRange
realRange
大于零{/ p>