android finish() method doesn't clear app from memory

49,937

Solution 1

Android keeps processes around in case the user wants to restart the app, this makes the startup phase faster. The process will not be doing anything and if memory needs to be reclaimed, the process will be killed. Don't worry about it :)

Solution 2

Best way is firstly use finish() and after that use System.exit(0) to clear static variables. It will give you some free space.

A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory

Its not true that is cousing problems i've tried it for over 3 years in my apps. Never get crashed or restart after using Exit()

Solution 3

Try using

System.exit(0);

Solution 4

Once onDestroy() gets called, your activity is doomed. Period.

That being said, the process (and hence address space) allocated to your application might still be in use by another part of your application -- another activity or service. It's also possible that your process is empty and the OS just hasn't gotten around to reclaiming it yet; it's not instant.

See the Process Lifecycle document for more information:
http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle

Regardless, if your activity is relaunched, it will have to go through the entire startup sequence again, starting with onCreate(). Do not assume that anything can implicitly be reused.

Solution 5

If you need to close application from subactivity, I may suggest you to made it in such a way: 1) from activity A call activity B as startActivityForResult(intent, REQUEST_CODE);

Intent intent = new Intent()
            .setClass(ActivityA.this, ActivityB.class);
            startActivityForResult(intent, REQUEST_CODE);

2) in activity A you should add method:

protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    if (requestCode == REQUEST_CODE) {
        if (resultCode == RESULT_CLOSE_APPLICATION) {
            this.finish();
        }
    }
}

3) in activity B call finish:

this.setResult(RESULT_CLOSE_APPLICATION);
this.finish();
Share:
49,937
Daniel Benedykt
Author by

Daniel Benedykt

Software Engineer

Updated on July 09, 2022

Comments

  • Daniel Benedykt
    Daniel Benedykt almost 2 years

    I have an activity and I call the finish() method and the activity is not cleared from memory.

    After calling finish() , I see that the method onDestroy() is executed successfully (and I clear all my variables and stuff in there).

    Should it be cleared from memory or its how android works? As I understand the LifeCycle of the Activity is finished.

    And if it keeps the app in memory so it runs faster the 2nd time the user uses it, what kind of objects can I leave in memory to reuse? If I understand correctly, I am suppose to clear everything on onDestroy.