I'm kind of new to Java but I am writing an Android app. Right now I'm working on an async task to calculate Pi but when I run it the memory usage increases alarmingly (+5MB per second). This one piece of code may need to run thousands of times so it is important that it be very optimized.
protected BigDecimal doInBackground(Object... params) {
digits = (int)params[0];
piView = (TextView)params[1];
boolean add = true;
BigDecimal pi = new BigDecimal(0.0);
for(int i=1; pi.setScale(digits, BigDecimal.ROUND_DOWN).equals(realPi) == false; i++){
if(add){
pi = pi.add(new BigDecimal(4.0 / (-1 + (i*2))));
add = false;
}
else {
pi = pi.subtract(new BigDecimal(4.0 / (-1 + (i*2))));
add = true;
}
}
return pi;
}
This one piece of code may need to run thousands of times so it is important that it be very optimized.