Reputation: 2128
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
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
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