Is there any reason开发者_运维问答 why an activity might not be finalized (i.e. have its finalize() called) and therefore garbage collected, even though its onDestroy() is called? Is this just your average everyday memory leak?
onDestroy
doesn't mean that the activity object was deleted, it just means that the activity itself (as an Android concept) was destroyed. This has nothing to do with the actual Activity object. Internally, the OS may decide to recycle it, especially when it's immediately recreated due to a configuration change (like the orientation changing from portrait to landscape).
Even IF it was garbage collected (which could happen much later after onDestroy, at a point way down the road when the system actually had to do a garbage collection because it needed memory), there's no guarantee finalize
gets called. It's even in the documentation:
"Note that the VM does guarantee that finalize() is called at most
once for any object, but it doesn't guarantee when (if at all)
finalize() will be called.".
Generally, you should never use finalize
unless you really have to. It's not as predictable as a destructor in C++. If you need to perform clean up in your activity, do it in onPause
or onDestroy
or any of the other hooks.
精彩评论