Damnum
Damnum

Reputation: 1859

Android Memory Leak? Bitmap exceeds VM budget error with MapView

I've written an application that creates a map activity. From there the user can switch to a menu and goes back to the map activity. After about 10 of those loops, the following error occurs:

02-28 21:35:54.780: E/AndroidRuntime(23502): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

I've tried the unbind drawables solution proposed here http://www.alonsoruibal.com/bitmap-size-exceeds-vm-budget/ and in various other threads but that did not help.

The only thing that helps is closing the map activity manually via finish(), but that causes an unnatural navigation behavior.

Here is my code:

MapActivity class

public class TestMapsForgeActivity extends MapActivity {

View mapView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mapView = new MapView(this);
}

@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.map_menu, menu);
    return true;
};

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    startActivity(new Intent(getApplicationContext(), MenuActivity.class));
    return true;
}
}

MenuActivity Class

public class MenuActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
};

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    startActivity(new Intent(getApplicationContext(), TestMapsForgeActivity.class));
    return true;

}

}

What I don't understand is, the garbage collector does apparently not destroy the MapActivity properly unless I close it with finish(). But shouldn't android call finish() by itself as soon as the application needs more memory?

Does anyone have some thoughts on this issue?

Thanks in advance!

Upvotes: 2

Views: 644

Answers (1)

Derzu
Derzu

Reputation: 7146

I think the problem is that you are starting an activity over another that wasn't closed.

Try this:

    Intent i = new Intent(getApplicationContext(), TestMapsForgeActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);

Setting the Intent flag CLEAR_TOP, will finish the others previous activitys, read more here: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

Upvotes: 2

Related Questions