سؤال

I've inherited a large Android app that was implemented using non-Android practices. The app has several dozen Activities and uses the app as a singleton for caching data. The app crashes after a long period of idle time - this can happen while the devices sits on a single Activity or when restarted from the recent history of apps menu (usually null refs issues.)

My question is - are there techniques or tools for faithfully forcing the resource freeing behavior to happen without waiting for the OS to reclaim the memory?

هل كانت مفيدة؟

المحلول

If you want to run the app through the wringer, under the device's Development Options, tick Don't keep Activities. However, this wouldn't address singleton storage under the Application class; the Application class will only be disposed once the app has been forced out of memory entirely.

نصائح أخرى

Firstly, if there is any state data you need to preserve you should make use of the built in life cycle methods and pull the caching behavior out of your Application class. Use the savedInstanceState Bundle and the Parcelable interface.

There are a couple things you can do to make sure resources get destroyed as the activities come and go. In onDestroy() (or onDestroyView() if you're in a fragment), set all your member variables to null. Avoid holding references to other activities. Android will call life cycle methods and attempt to reclaim memory. New instances of the same activity can be created at a future time, but the old reference may linger because it's still being pointed to.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top