ArrayList.add() 导致 OutOfMemory 错误?
ArrayList.add() causing OutOfMemory error?
今天我第一次遇到 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);
这个错误是不是因为使用了 Long 数字格式?欢迎提出任何建议。
我想我已经确定了问题所在。
我可能在这一行创建了一个无限循环
for (int i=0;i<i+realRange;i++){
SMAs.add(i,0L);
}
看起来像无限循环:
for (int i=0;i<i+realRange;i++)
i
将始终小于 i+realRange
因为 realRange
大于零:
今天我第一次遇到 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);
这个错误是不是因为使用了 Long 数字格式?欢迎提出任何建议。
我想我已经确定了问题所在。
我可能在这一行创建了一个无限循环
for (int i=0;i<i+realRange;i++){
SMAs.add(i,0L);
}
看起来像无限循环:
for (int i=0;i<i+realRange;i++)
i
将始终小于 i+realRange
因为 realRange
大于零: