I have a problem in some devices like Galaxy Nexus, where if you keep opening activities, you hit out of memory error. I thought I had some memory leaks which prevent activities from collected, but I couldn't find it. So I wrote this small activity (purely for test purpose.)
public class Test extends Activity {
private byte[] imageData = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button iv = new Button(this);
imageData = new byte[1024 * 1024 * 2];
iv.setText("Open");
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Test.this, Test.class));
}
});
setContentView(iv);
}
}
So basically, it allocates 2MB of memory, and you can open another instance of the same activity. On Galaxy S and Kindle Fire, if you keep opening, memory usage increases to a certain point, and then it starts destroying old activities for new activities.
However on Galaxy Nexus, it just goes up until 64MB and crashes with out of memory error.
So is there something I do not know about android memory management, or is this a bug on some devices? If it's a bug, how can I work around it?
Thank you.