Android not killing activities from stack when memory is low
Asked Answered
P

6

10

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];
    }
}

Phox answered 23/7, 2012 at 16:26 Comment(0)
B
14

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.

Botulin answered 23/7, 2012 at 16:45 Comment(3)
Thanks! For the moment we will use the proposed flag FLAG_ACTIVITY_REORDER_TO_FRONT and implement the onNewIntent callback to make sure the activity is updated with the new intent.Phox
@commonsware , the doc does say that a specific Activity can be killed from the stack. "A paused activity is completely alive (...), but can be killed by the system in extreme low memory situations." and "(a stopped Activity) still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere." (link developer.android.com/reference/android/app/Activity.html). That's exactly how the "Don't keep Activity" tool works also...Kerouac
@Alesqui: Android will only do so in the act of terminating the entire process.Botulin
C
1

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.

Classified answered 23/7, 2012 at 16:31 Comment(1)
Thanks, your solution should work but it also remove the destroyed activities from the stack. I'll follow CommonsWare advice since the history is kept with at least one activity of its kind.Phox
H
0

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

Heins answered 5/10, 2012 at 10:53 Comment(1)
But as the accepted answer from several months ago points out, this is not the case.Unintelligent
T
0

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.

Testate answered 17/7, 2017 at 10:59 Comment(0)
G
0

CommonsWare's answer is correct, besides you can try this strategy to release the memory of the activity at the bottom stack:

  • Remove alls fragment and/or setContentView(null)
  • onResume: call Activity#recreate(), or just simply create new fragment instance and add it back to your view hierarchy.

One important thing, don't rely on these events onLowMemory, onTrimMemory. Because they're invoked only when the entire system runs on low memory, your app can run out of memory before that. You have to track memory usage of your process and take appropriate action if it ate too much memory:

val maxMemory = Runtime.getRuntime().maxMemory()
val freeMemory = Runtime.getRuntime().freeMemory()
val totalMemory = Runtime.getRuntime().totalMemory()
val remainMemoryPercent = (freeMemory + maxMemory - totalMemory) * 100f / maxMemory

// check remainMemoryPercent and take action
Guelph answered 13/4, 2020 at 2:48 Comment(0)
D
0

For the question, We can not tell the Android OS to do that. Let think, the system has many applications, if every tell that, it is bad. But the system gives us the notification, we can do that in this method.

//in applicaion class
public void onTrimMemory (int level) {
  //according to different level do different things.
  //for example TRIM_MEMORY_UI_HIDDEN to finish old activities 

}

we can also let the activity implement ComponentCallbacks, do it in its own class onLowMemeroy, then when the activity starts to register it, destroy unregister. we can use ActivityLifecycleCallbacks to auto realize it.

//in application class
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks(){

   void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState);{
if(activity instanceOf ){
    registerComponentCallbacks((ComponentCallbacks)activity)
  }
}
 
...
void onActivityDestroyed(@NonNull Activity activity);{
  if(activity instanceOf ){
    unregisterComponentCallbacks((ComponentCallbacks)activity)
  }

}


})

// in activity implement ComponentCallbacks, override onlowMemeroy method
void onLowMemory();{
  //finish or remove some resouce
}

As for why Android OS can not allocate that, that is because OS has two ways for memory allocation.

One way is java garbage allocation. which does the object allocation.

The other way is low memory kill, to ensure the process has enough memory. when memory is not enough for running the application, it will kill other applications in the background.

Your activity in java garbage allocation way can not allocate, because it is held by ActivityThread. so it can not allocation.

To free memory, we can finish the old activities when receiving the low memory notification.

Duax answered 24/12, 2021 at 0:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.