Amit Vikram Singh
Amit Vikram Singh

Reputation: 2128

Difference between Activity destroyed and process killed

  1. When onDestroy method is called then only activity is destroyed or process is killed?
  2. When system destroys activity and when it kills process?
  3. what is difference between activity is destroyed and process is killed? Should I clear my ArrayList when activity is destroyed(or it will be marked by system for garbage collection when activity is destroyed)? and If I have to clear ArrayList(in onDestory method), what will happen if system kills app without calling onDestroy.

I am using onDestroy method to close a cursor.

@Override
protected void onDestroy() {
    super.onDestroy();
   // closing cursor!
   mCursor.close();
}

I press home button and app goes in background. If System kills app without calling onDestroy method, will there be any memory leak because I am not able to close cursor(which I close in onDestroy method)

Upvotes: 1

Views: 1106

Answers (2)

emandt
emandt

Reputation: 2706

The JVM will "clean" all the memory used during your App when its process ends. This means if the Process is killed it's not needed to free all objects/memory allocated during its execution.

Moreover: killing a process doesn't triggers any "onDestroy()" event and neither the "finally" part of a "try {}" block but it stops the App exactly in that moment without let the App doing nothing else.

Special things like Cursors are managed by the JVM that will releases them when the Process ends (using a normal command or via a "kill process" procedure), so NO ANY memory leak could happen AFTER a killing process procedure. Same behavior occurs in JNI when you allocate a memory block (using "malloc()").

A memory leak occurs only while your App is still running inside the JVM, but not when it is killed.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006869

When onDestroy method is called then only activity is destroyed

Yes.

...or process is killed?

No.

When system destroys activity

Android will destroy an activity when it is finished, such as when the user presses the BACK button (assuming nothing else handles that BACK press). Android will destroy visible activities when the device has undergone a configuration change since the activity became visible (assuming that you have not disabled this in the manifest). Android may destroy activities as part of terminating a process, but this is not guaranteed.

when it kills process?

Android will terminate processes as needed to free up system RAM, when the amount of free system RAM gets low. See the documentation for more.

what is difference between activity is destroyed and process is killed?

They are not closely related.

Should I clear my ArrayList when activity is destroyed

We have no idea what ArrayList you are referring to.

If System kills app without calling onDestroy method, will there be any memory leak because I am not able to close cursor(which I close in onDestroy method)

No. Once a process is terminated, all memory associated with that process is freed.

Upvotes: 3

Related Questions