Low Memory

Your application is not alone. It has to share resources with many other applications and also the system as a whole. Consequently, there may be times when there is not enough memory for everyone and in this case, Android will ask applications and applications' components (such as activities or fragments) to tighten their belts.

The ComponentCallbacks interface defines the onLowMemory() API, which is common to all application components. When it is called, a component is basically asked to release objects it does not really need. Typically, your implementation of onLowMemory() would release:

  • Caches or cache entries (for example, LruCache as it uses strong references)
  • Bitmap objects that can be generated again on demand
  • Layout objects that are not visible
  • Database objects

You should be careful about deleting objects that are costly to recreate. However, not releasing enough memory may cause Android to be more aggressive and start killing processes, possibly even your own application. If your application is killed, then it will have to start from scratch again the next time the user wants to use it. Consequently, your application should play nice and release as many resources as it can, because it should benefit not only other applications but also your own. Using lazy initializations in your code is a good habit; it allows you to implement onLowMemory() later without having to modify the rest of your code significantly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.24.118