A project with only the following two activities will throw an OutOfMemoryError. Why does this happen? Shouldn't the older activities on the stack be released when memory is nearing the limit of the heap?
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(getApplicationContext(), MemoryActivity.class));
}
}
public class MemoryActivity extends Activity
{
int[] testMem = new int[750000 * 3];
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
}
It seems the consensus is that the documentation is out-of-date or misleading, because if an activity's process is in the foreground, that activity will not be killed even if it is stopped. An activity will be killed as part of its entire process being killed.
Please check out this question, answer by Dianne Hackborn, and discussion: Android app out of memory issues - tried everything and still at a loss