Android Finish() Method Doesn't Clear App from Memory

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

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 :)

Will calling finish() from an activity free up my memory space?

The activity you're calling the finish() method from is destroyed and all its resources are queued for garbage collection, because a reference to this activity becomes inaccessible. So, all memory that was used by this activity will be freed during next GC cycle.

Release Memory of Particular Activity when it is Destroyed

Add following code for it

@Override
protected void onDestroy() {
//android.os.Process.killProcess(android.os.Process.myPid());

super.onDestroy();
if(scaledBitmap!=null)
{
scaledBitmap.recycle();
scaledBitmap=null;
}

}

What is Activity.finish() method doing exactly?

When calling finish() on an activity, the method onDestroy() is executed. This method can do things like:

  1. Dismiss any dialogs the activity was managing.
  2. Close any cursors the activity was managing.
  3. Close any open search dialog

Also, onDestroy() isn't a destructor. It doesn't actually destroy the object. It's just a method that's called based on a certain state. So your instance is still alive and very well* after the superclass's onDestroy() runs and returns.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

Will finish in an activity kill all the objects in it

  1. Whenever the object don't have any references you can declare it as null.

    1. You can verify the memory information by using dumpsys meminfo (pid) command inside adb shell

    2. If you cal manually gc it will not give you a guaranty of garbage collection

Android clear activity from memory

 android:allowTaskReparenting="false"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:permission="android.permission.USE_SIP"
android:screenOrientation="portrait"
android:taskAffinity=""

Try adding above properties to activity in manifest

What happens when all activities of an application finishes?

1) No, Android does not guarantee so. It's up to the OS to decide whether to terminate the process or not.

2) Because the Activity instance still lives in the Dalvik VM. In Android each process has a separate Dalvik VM.

Each process has its own virtual machine (VM), so an application's
code runs in isolation from other applications.

When you call finish() this doesn't mean the Activity instance is garbage collected. You're telling Android you want to close the Activity (do not show it anymore). It will still be present until Android decides to kill the process (and thus terminate the DVM) or the instance is garbage-collected.

Android starts the process when any of the application's components
need to be executed, then shuts down the process when it's no longer
needed or when the system must recover memory for other applications.

3) I wouldn't do so unless you have some very strong reason. As a rule of thumb, you should let Android handle when to kill your application, unless there's something in your application state that requires an application reset when it loses focus.

Quotes source



Related Topics



Leave a reply



Submit