Android OnLowMemory and OnTrimMemory

Source: Internet
Author: User

Android OnLowMemory and OnTrimMemory

 

 

 

1. OnLowMemory

OnLowMemory is an API provided by Android. When the system memory is insufficient and all background programs (processes with the background priority, not those running in the background) are killed, the system calls OnLowMemory. The system provides the following callbacks: Application/Activity/Fragementice/Service/ContentProvider.

In addition to the APIS provided by the above system, you can also implement ComponentCallbacks by yourself and register through the API to get the OnLowMemory callback. For example:

Public static class MyCallback implements ComponentCallbacks {

@ Override

Public void onConfigurationChanged (Configuration arg ){

}

 

@ Override

Public void onLowMemory (){

// Do release operation

}

}

Then, use Context. registerComponentCallbacks () to register the callback when appropriate. With this custom method, callback can be registered in many places without being limited to the components provided by the system.

2. OnTrimMemory

OnTrimMemory is an API provided after Android 4.0. The system calls back and forth based on different memory states. The system provides the following callbacks: Application/Activity/Fragement/Service/ContentProvider.

The OnTrimMemory parameter is an int value, representing different memory states:

  • TRIM_MEMORY_COMPLETE: the memory is insufficient, and the process is located at the end of the background process list and will be cleared immediately.

  • TRIM_MEMORY_MODERATE: the memory is insufficient and the process is in the middle of the background process list.

  • TRIM_MEMORY_BACKGROUND: the memory is insufficient and the process is a background process.

  • TRIM_MEMORY_UI_HIDDEN: the memory is insufficient and the UI of the process is invisible.

    The four above are 4.0 more

    •  

    • TRIM_MEMORY_RUNNING_CRITICAL

    • TRIM_MEMORY_RUNNING_LOW: insufficient memory (less than five background processes), and the priority of this process is relatively high, you need to clear the memory

    • TRIM_MEMORY_RUNNING_MODERATE: the memory is insufficient (more than five background processes), and the priority of the process is relatively high. The memory needs to be cleared.

    • The preceding three values increase by 4.1.

      The system also provides ComponentCallbacks2, which is called back by the system after being registered through Context. registerComponentCallbacks.

      Comparison between OnLowMemory and OnTrimMemory

      1. When OnLowMemory is called back, there are no background processes. When onTrimMemory is called back, there are background processes.
      2. OnLowMemory is called when the last backend process is killed. Generally, it is triggered after the low memory killer process is killed. OnTrimMemory is triggered more frequently. When the priority of a process is calculated, it is triggered if the conditions are met.
      3. After one-click cleanup, OnLowMemory will not be triggered, while OnTrimMemory will be triggered once.


       

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.