Android Code memory optimization recommendations Ontrimmemory

Source: Internet
Author: User

The Ontrimmemory callback is an API provided after Android 4.0, which is provided to developers, and its main purpose is to prompt the developer to free up memory by processing some of the resources when the system is out of memory, thus avoiding being killed by the Android system. This applies at the next boot time, the speed will be relatively fast.

What is the function of Ontrimmemory callback?

Ontrimmemory is a callback that Android added after 4.0, and any class that implements the ComponentCallbacks2 interface can override this callback method. The main function of Ontrimmemory is to instruct the application to release its own memory in different situations to avoid being killed directly by the system and to improve the user experience of the application.

The Android system will call this function according to different levels of memory usage, and pass in the corresponding level:

Trim_memory_ui_hidden indicates that all UI interfaces of the application are hidden, that is, the user clicked the home key or the back key to make the UI interface of the app invisible. Some resources should be released at this time.

Trim_memory_ui_hidden This grade is more commonly used, and the following six relationships are not very strong, so alone said

The following three levels are callbacks when our application really runs:

Trim_memory_running_moderate indicates that the application is functioning properly and will not be killed. But now that the phone's memory is a bit low, the system may start to kill the process according to the LRU cache rules.

Trim_memory_running_low indicates that the application is functioning properly and will not be killed. But now that the phone's memory is very low, we should release some unnecessary resources to improve the performance of the system, and this will directly affect the performance of our application.

Trim_memory_running_critical indicates that the application is still working, but the system has killed most of the cached processes according to the LRU cache rules. At this time we should try to release any unnecessary resources, otherwise the system may continue to kill all the processes in the cache, and start to kill some of the processes that should be kept running, such as services running in the background.

When the application is cached, you receive the following types of callbacks:

Trim_memory_background says the phone is currently low on memory and the system is ready to start cleaning up the process based on the LRU cache. This time our program in the LRU cache list of the nearest location, is not likely to be cleaned out, but then to release some of the more easily recovered resources can make the phone's memory more abundant, so that our program is kept in the cache for a longer time, so that when the user returned to our program will feel very smooth, Instead of going through a reboot process.

Trim_memory_moderate said the phone is currently low in memory, and our program is in the middle of the LRU cache list, if the phone memory is not further released, then our program has been killed by the system risk.

Trim_memory_complete said the phone is currently low in memory, and our program is at the very edge of the LRU cache list, the system will give the highest priority to kill our application, at this time should be as much as possible to release everything that can be released.

Which components can implement Ontrimmemory callbacks?

Application.ontrimmemory ()

Activity.ontrimmemory ()

Fragement.ontrimmemory ()

Service.ontrimmemory ()

Contentprovider.ontrimmemory ()

What resources can be freed in the Ontrimmemory callback?

Usually in the architecture phase to consider clearly, we have what is to be resident memory, and what is the accompanying interface exists in general circumstances, there are several resources need to be released:

The cache cache includes some file caches, image caches, and so on, which are useful when users are working properly, but when your application UI is not visible, these caches can be erased to reduce memory usage. such as the caching of third-party picture libraries.

Some dynamically generated view that is dynamically added. These dynamically generated and added view, which is used only in a few cases, can be released and then dynamically generated the next time you use it. Native desktops, for example, release all Appscustomizepagedview resources in the Ontrimmemory trim_memory_moderate hierarchy to ensure that the desktop is not easily killed when memory is low.

Example: Clearing the cache

@Override

public void Ontrimmemory (Int. level) {

if (level >= componentcallbacks2.trim_memory_moderate) {

Clear the caches. Note all pending requests'll be removed too.

Clear ();

}

}

public void Clear () {

Mpendingrequests.clear ();

Mbitmapholdercache.evictall ();

Mbitmapcache.evictall ();

}

Why would you call ontrimmemory?

Although the system kills the process in low memory, the sequence is lower to highest in the LRU cache, but it also considers killing applications that use high memory to get more memory faster.

So if your application occupies less memory, you can increase the chance of not being killed, so that quickly recover (if not killed, the start is hot start, or cold start, its speed is less than a few times the difference).

So releasing your UI resources in several different ontrimmemory callbacks can effectively improve the user experience.

http://mp.weixin.qq.com/s?__biz=MzAwNjE5MzA0NQ==&mid=211317365&idx=1&sn= C24d1be87cfe9e2ef341c2347dd5f61b&scene=1#rd

Android Code memory optimization recommendations Ontrimmemory

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.