Android Ontrimmemory () and Onlowmemory ()

Source: Internet
Author: User

1. Onlowmemory ()

Onlowmemory () is an Android-provided API that calls Onlowmemory when the system is out of memory and all background programs (priority background processes, not processes running in the background) are killed. The callbacks provided by the system are: application/Activity/fragementice/service/contentprovider

In addition to the above system-provided API, you can also implement the Componentcallbacks, through the API registration, so you can also get onlowmemory callback. For example:

Public static class Mycallback implements Componentcallbacks {

@Override

public void onconfigurationchanged (Configuration arg) {

}

@Override

public void Onlowmemory () {

Do release operation

}

}

Then, the callback can be registered by Context.registercomponentcallbacks () at the appropriate time. With this custom method, callbacks can be registered in many places without having to be confined to system-provided components.

2. Ontrimmemory ()

Ontrimmemory () is an API that is provided after Android 4.0, and the system is tuned back and forth depending on the memory state. Callbacks provided by the system are: application/activity/fragement/service/contentprovider

  The ontrimmemory parameter is an int value that represents a different memory state:

    • trim_memory_complete: Out of memory and the process is at the end of the list of background processes and will be cleaned up immediately

    • trim_memory_moderate: Not enough memory, and the process is in the middle of the list of background processes.

    • trim_memory_background: Not enough memory, and the process is a background process.

    • trim_memory_ui_hidden: There is not enough memory and the UI of the process is not visible.       

Above 4 is 4.0 increase


  • Trim_memory_running_critical: Insufficient memory (less than 3 background processes), and the process has a high priority and needs to clean up memory

  • Trim_memory_running_low: Insufficient memory (less than 5 background processes), and the process has a high priority and needs to clean up memory

  • Trim_memory_running_moderate: Insufficient memory (more than 5 background processes), and the process has a high priority and needs to clean up memory

  • Above 3 is 4.1 increase

The system also provides a ComponentCallbacks2, after registering with Context.registercomponentcallbacks (), it will be recalled to the system.

Comparison of Onlowmemory and Ontrimmemory

1,onlowmemory is callback, there is no background process, while Ontrimmemory is callback, there is a background process.
2,onlowmemory is called when the last background process is killed, the general situation is triggered after the low memory killer kill process, and the Ontrimmemory trigger is more frequent, each time the process priority is evaluated, it is triggered whenever the condition is met.
3, Onlowmemory will not be triggered by one-click Cleanup, and ontrimmemory will be triggered once.

Android Ontrimmemory () and Onlowmemory ()

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.