I think my app is taking up extra memory that the GC should be able to reallocate. I don't know if these will be considered memory leaks, but there are 2 places that I have noticed possible issues
From App start, continuously rotate my device from portrait to landscape to portrait to landscape....
- +300KB cumulative memory usage per rotate
With 2 inputs, every button click
- +30KB cumulative memory usage per click
This issue is that the memory is never released as long as the app is still in view.
Example: Rotate the device 10 times, and click the buttons 50 times -> consumes 4.5MB memory. If I leave the app open and don't do anything for 1 hour, then my app will still consume 4.5MB of memory; even though a lot of the memory should have been released around 59 minutes earlier!!
My concern is why the memory is never released while the app is always in view?
Am I wrong in how this works?
NOTE: The app is called ContrivedCalculator
Code
UI
public class Calculator extends AppCompatActivity implements ICalculatorInteraction {
private EditText txtNumber1, txtNumber2, txtResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
Button btnAdd = (Button) findViewById(R.id.btnAddition);
Button btnSub = (Button) findViewById(R.id.btnSubtract);
Button btnMul = (Button) findViewById(R.id.btnMultiple);
Button btnDiv = (Button) findViewById(R.id.btnDivide);
txtNumber1 = (EditText) findViewById(R.id.txtNumber1);
txtNumber2 = (EditText) findViewById(R.id.txtNumber2);
txtResult = (EditText) findViewById(R.id.txtResult);
btnAdd.setOnClickListener(new OperationClick(Add).listenerOn(this));
btnSub.setOnClickListener(new OperationClick(Subtract).listenerOn(this));
btnMul.setOnClickListener(new OperationClick(Multiply).listenerOn(this));
btnDiv.setOnClickListener(new OperationClick(Divide).listenerOn(this));
@Override
public String getFirstNumber() { return valueOf(this.txtNumber1); }
@Override
public String getSecondNumber() { return valueOf(this.txtNumber2); }
@Override
public void updateResult(String result) { this.txtResult.setText(result); }
private String valueOf(EditText textbox) {
String text = textbox.getText().toString();
if (text.isEmpty()) {
textbox.setText("0");
return "0";
}
return text;
}
// default android activity methods
}
Listener Logic
public class OperationClick {
private BinaryOperation operation; // ENUM - advanced
private View.OnClickListener listener;
public OperationClick(final BinaryOperation operation) { this.operation = operation; }
public View.OnClickListener listenerOn(final ICalculatorInteraction UI) {
if (listener != null) return listener;
return listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
double num1, num2, total;
String result, sign;
num1 = Double.parseDouble(UI.getFirstNumber());
num2 = Double.parseDouble(UI.getSecondNumber());
total = operation.execute(num1, num2);
sign = operation.getSymbol();
result = String.format("%s %s %s = %s", num1, sign, num2, total);
UI.updateResult(result);
}
};
}
Calculation Logic
public enum BinaryOperation {
Add ("+") { @Override double execute(final double a, final double b) { return a + b; } },
Subtract ("-") { @Override double execute(final double a, final double b) { return a - b; } },
Multiply ("×") { @Override double execute(final double a, final double b) { return a * b; } },
Divide ("÷") { @Override double execute(final double a, final double b) { return a / b; } };
private final String symbol;
abstract double execute(double a, double b);
BinaryOperation(String symbol) { this.symbol = symbol; }
public String getSymbol() { return this.symbol; }
}