We've been developing an application that has a drop down dashboard that allows the users to navigate throughout the app. The navigation is not very standard since this menu can be accessed from almost every activity. After playing for a while opening activities using the menu, the stack starts to grow and grow.
All these activities contain listviews with several imageviews inside, and they take around 3mb each. If the user plays enough and creates more than 25 activities on the stack this is what happens:
- Out of memory error is thrown (Heap is increased till there's no more heap).
- A dialog is shown due to the exception (Unfortunately, %activity% has stopped.)
- The activity where the outofmemerror was thrown is finished.
- All the activities in the stack are finished, but the history is kept, so its possible to backup and each activity is recreated automaticall by the OS.
I was expecting the system to kill the oldest activities in the stack automatically BEFRORE the OutOfMemoryError was thrown...
Just to be sure the OS is not killing old activities, I created a test app that allocates 1mb each time. Guess what: The behavior is the same and outofmemerror is thrown:
The question is: How can we tell the Android OS that it is allowed to deallocate activities and its resources if needed so we don't get the "Unfortunately, your activity has stopped." dialog?
Proof of concept
package com.gaspar.memorytest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MemoryTestActivity extends Activity {
/** Called when the activity is first created. */
private byte[] mData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
((Button)findViewById(R.id.button)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MemoryTestActivity.this, MemoryTestActivity.class);
startActivity(i);
}
});
mData = new byte[1*1024*1024];
}
}