简体   繁体   中英

Activity onDestroy is never called before OutOfMemoryError

In my android application the onDestroy method on the Activity is never called before the application runs out of memory. I can call finish() on the onStop() method of the activity and will never run out of memory, however then I lose the back button functionality.

Sorry I can't be any more specific about the issue. Everything I have read is that android is supposed to call the onDestroy() and finish() when resources get low however this is never called.

I would like the activities at the bottom of the stack to have finish() called on them. They aren't and I eventually run out of memory.

I call: adb shell dumpsys meminfo com.mypackage

And I see the activity count always incrementing until the OutOfMemoryError. If I call finish() on the activities at onStop() then the activity count will stay around 30 and no OOME.

Thanks for any advice. Memory management on android is a disaster, thanks for tips you have.

Everything I have read is that android is supposed to call the onDestroy() and finish() when resources get low however this is never called.

There is no guarantee that onDestroy() will ever get called by the system... the operating system has the ability to kill any process it pleases without warning at any time it wants. This was an intentional design decision, as it ensures the system remains in complete control of the device even in the presence of malicious applications.

Memory management on android is a disaster.

The system is very good at destroying background activities to make room for others when memory becomes low. You shouldn't have to worry about how many activities are currently in the back stack and you certainly shouldn't be holding weak references to these activities just so you can manually call finish() on them at a later time. The system will do all of this for you.

If you are getting an OutOfMemoryError then that means either your application has a severe memory leak that you need to fix, or your application is allocating huge chunks of memory at a time until the runtime can no longer take it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM