3

I have a simple Hello World application

public class TestLeaksOnFinish extends Activity  
{
    static int ctr = 0;
    protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    TextView t = new TextView(this);
    t.setText("Hello World! "+ctr++);
    setContentView(t);          
    }   
}

When I run this multiple times, each time followed by pressing BACK, I see that ctr increases each time, indicating that the Activity is not killed completely after BACK.

This is also confirmed by dumping the HPROF file in DDMS after pressing BACK.
This file still contains my TestLeaksOnFinish activity class.

Can someone explain why this Activity is still present in the heap dump after pressing BACK?

When I list the incoming references, I get the following screenshot from DDMS

Serdar Dogruyol
  • 5,147
  • 3
  • 24
  • 32
Marc Van Daele
  • 2,856
  • 1
  • 26
  • 52
  • 1
    Are you sure that `finish()` is being called when you press the back key? Maybe Android just pauses the application and resumes it afterwards. Besides, `ctr` is a static field - they're known for leaking. – Michell Bak Nov 16 '11 at 09:05
  • A static primitive won't leak the Activity – FunkTheMonk Nov 16 '11 at 09:28
  • @MichellBak finish() gets called (it's the default implementation of onBackPressed() but I override it just to be sure). onPause and onDestroy are also called. Also if I make ctr not static anymore, I get the same data in the heap dump. When running the app multiple times, ctr itself is always zero indicating that a new instance is created each time (as expected) which still makes me wonder why the old one is kept in memory since it won't be used anymore. – Marc Van Daele Nov 16 '11 at 09:28
  • Like i said in the post the application goes to background and cached to memory it's not being killed.And when you start again it'll be just reloaded from the memory depending on your settings. – Serdar Dogruyol Nov 16 '11 at 09:32
  • A static variable has no reference to a specific instance of its class, so keeping a reference to the static variable can't leak an instance of the Activity. Likewise a primitive can't hold a reference to anything so it can't retain another object and stop it from getting GCed. A static object could potentially leak an activity if it retains an instance to a context/activity - for example a Drawable's call back is set to the context of the activity if it is ever used in an ImageView - hence why a static Drawable is bad (since the lifetime of static is FOREVERRRRR!!! (or not)). – FunkTheMonk Nov 16 '11 at 10:08

1 Answers1

11

In Android activities dont get killed they'll just be moved to background.That's how Android works you cant kill an Application or basically there's not quit as you know.It'll just stay in the background and in the memory.

When you run out of memory then the system starts to kill the Application processes according to priority that the system gives to every application itself.

Serdar Dogruyol
  • 5,147
  • 3
  • 24
  • 32
  • I understand this general behavior when an application contains services, content providers etc. However, in my example, I only had a single Activity which was finished and (especially when removing the static) the activity would not be reused anymore (another instance of the activity will be created on restart) so I was surprised to see that the heap dump still contained a reference to my activity. – Marc Van Daele Nov 16 '11 at 10:12
  • That's how the Android system behaves sounds kinda surprising at first but whenever you get used to it you see that it's actually how it should be. – Serdar Dogruyol Nov 16 '11 at 10:15
  • Is the following more or less correct: in Android an application consists of activities, content providers and services; it **is** possible to kill (=finish()) an Activity but it is not possible to kill/quit the whole Application. Hence, even when the last Activity is finished, Android will still keep a reference to the Application. This reference happens to be my (already finished) Activity. – Marc Van Daele Nov 16 '11 at 19:11
  • If I understand correctly, all but the very first activity will actually really be removed from memory with finish()? So all I have to do is put finish()? Then one could have a dummy first activity without any meaning or variables, there it does not matter. ALL the other more complicated activities will than surely be removed from memory on finish()? Is this correct? – user387184 Dec 08 '11 at 18:20