Android not killing activities from stack when mem

2020-02-28 06:05发布

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:

  1. Out of memory error is thrown (Heap is increased till there's no more heap).
  2. A dialog is shown due to the exception (Unfortunately, %activity% has stopped.)
  3. The activity where the outofmemerror was thrown is finished.
  4. 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]; } }

4条回答
别忘想泡老子
2楼-- · 2020-02-28 06:48

I was expecting the system to kill the oldest activities in the stack automatically BEFRORE the OutOfMemoryError was thrown

Android does not do this. Android terminates processes to free up system memory for other processes. It does not get involved with intra-app memory usage in a similar fashion.

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?

You can't.

Instead, you need to design your application to use fewer activities, or use fewer resources per activity. For example, you can "recycle" existing activity instances via FLAG_ACTIVITY_REORDER_TO_FRONT, or (as Mr. Tornquist pointed out), you can finish() activities manually yourself.

After playing for a while opening activities using the menu, the stack starts to grow and grow.

You should probably be using FLAG_ACTIVITY_REORDER_TO_FRONT on these menu items, so that you bring the existing activity forward in the task, rather than creating new ones each time.

查看更多
够拽才男人
3楼-- · 2020-02-28 06:50

I was also under the impression that the system will kill old activities to free memory, according to android developer guide:

"When the system stops one of your activities (such as when a new activity starts or the task moves to the background), the system might destroy that activity completely if it needs to recover system memory. When this happens, information about the activity state is lost. If this happens, the system still knows that the activity has a place in the back stack, but when the activity is brought to the top of the stack the system must recreate it (rather than resume it). In order to avoid losing the user's work, you should proactively retain it by implementing the onSaveInstanceState() callback methods in your activity."

see link: android activities

查看更多
做自己的国王
4楼-- · 2020-02-28 06:54

Calling finish() when you are starting the new activity will deallocate the one you are leaving. This will prevent you from accessing it with the back button, but it should keep memory down.

查看更多
走好不送
5楼-- · 2020-02-28 06:54

Use the Intent Flag FLAG_ACTIVITY_REORDER_TO_FRONT

 Intent i = new Intent(ActivityD.this, ActivityA.class);
  i.setFlags(FLAG_ACTIVITY_REORDER_TO_FRONT);
 startActivity(i);

This will simply bring ActivityA to the front of the stack and leave B and C where they are which I believe is what you want. Then you can obviously call finish() on D if you want to remove it from the stack.

查看更多
登录 后发表回答