Android Strum Onlowmemory () and Ontrimmemory ()

Source: Internet
Author: User

See Guo Go Today Litepal framework of the source code, just open litepalapplication inside the source see such a scene

@Overridepublic void Onlowmemory () {super.onlowmemory (); mcontext = Getapplicationcontext ();}

I don't know the meaning of Guo Go. Before vaguely remember someone said Onlowmemory () and ontrimmemory (), so, I went to check the source, this blog to Strum onlowmemory () and Ontrimmemory ()


First through the Guo Go code, you see, the following parts

public void Onlowmemory () {        object[] callbacks = Collectcomponentcallbacks ();        if (callbacks! = null) {for            (int i=0; i<callbacks.length; i++) {                ((componentcallbacks) callbacks[i]). Onlowmemory ();}}}    

An interface callback, continue to see Onlowmemory ()

/** * This was called when the overall system was running low on memory, and * actively running processes should tri  m their memory usage. While * The exact point at which this would be called was not defined, generally * it would happen when all Backgroun     D process has been killed. * That's, before reaching the point of killing processes hosting * service and foreground UI that we would like to AV     OID killing. * * <p>you should implement this method to release * Any caches or other unnecessary resources     Lding on to.     * The system would perform a garbage collection for your after returning from this method. * <p>preferably, should implement {@link Componentcallbacks2#ontrimmemory} from * {@link COMPONENTCALLBACKS2  } to incrementally unload your resources based on various * levels of memory demands. That API was available for API level higher and should * only use this {@link #onLowMemory} method as a Fallback for older versions, which can is * treated the same as {@link componentcallbacks2#ontrimmemory} with the {@l Ink * Componentcallbacks2#trim_memory_complete} level.</p> */void onlowmemory ();

I go, so many English comments, in fact, People's English comments written is very clear, onlowmemory () is in the memory is relatively tense, according to the priority of the background program to kill, the system callback him, it used before 14, 14 after the appearance of Ontrimmemory ()

public void Ontrimmemory (Int. level) {        object[] callbacks = Collectcomponentcallbacks ();        if (callbacks! = null) {for            (int i=0; i<callbacks.length; i++) {                Object c = callbacks[i];                if (c instanceof ComponentCallbacks2) {                    ((COMPONENTCALLBACKS2) c). Ontrimmemory (level);}}}    


/** * level for {@link #onTrimMemory (int.)}: The process is nearing the end * of the background LRU list, and if Mo     Re memory isn ' t found soon it'll * be killed.        */static final int trim_memory_complete = 80; /** * level for {@link #onTrimMemory (int.)}: The process is around the middle * of the background LRU list;     Freeing memory can help the system keep * and processes running later in the list for better overall performance.        */static final int trim_memory_moderate = 60;  /** * level for {@link #onTrimMemory (int)}: The process had gone on to the * LRU list.  This was a good opportunity to clean up resources, can * efficiently and quickly be re-built if the user returns to     The app.        */static final int trim_memory_background = 40; /** * level for {@link #onTrimMemory (int.)}: The process had been showing * a user interface, and is no longer doin  G So. Large allocations with * The UI should is relEased at this point to allow memory to be better * managed.    */static final int trim_memory_ui_hidden = 20; /** * level for {@link #onTrimMemory (int)}: The process was not a expendable * background process, but the device     Is running extremely low on memory * and are about-not being able to keep any background processes running. * Your running process should free up as many non-critical resources as it * Can-allow the memory to be used Elsew  Here. The next thing that * would happen after this is {@link #onLowMemory ()} called     Be kept in the background, a situation that can start * to notably impact the user.    */static final int trim_memory_running_critical = 15; /** * level for {@link #onTrimMemory (int)}: The process was not a expendable * background process, but the device     is running low on memory. * Your running process should free up unneeded resources to allow that * memory to be used elsewhere.    */static final int trim_memory_running_low = 10; /** * level for {@link #onTrimMemory (int)}: The process was not a expendable * background process, but the device     Is running moderately low on memory.     * Your running process may want to release some unneeded resources for * use elsewhere.    */static final int trim_memory_running_moderate = 5; /** * Called when the operating system had determined that it was a good * time for a process to trim unneeded memo  Ry from its process. This would * happen for example if it goes in the background and there are not enough * memory to keep as many BAC  Kground processes running as desired. You * should never compare to exact values of the level, since new intermediate * values May is added--you'll     Typically want to compare if the value is greater, or equal to a level, you were interested in. * * <p>to Retrieve the processes current trim level @ any POint, can * use {@link android.app.activitymanager#getmymemorystate * activitymanager.getmymemorystate (Running     Appprocessinfo)}. * * @param level the context of the trim, giving a hint of the amount of * trimming the application may like to PE  Rform. May be * {@link #TRIM_MEMORY_COMPLETE}, {@link #TRIM_MEMORY_MODERATE}, * {@link #TRIM_MEMORY_BACKGROUND}, {@link # Trim_memory_ui_hidden}, * {@link #TRIM_MEMORY_RUNNING_CRITICAL}, {@link #TRIM_MEMORY_RUNNING_LOW}, * or {@link #TR     Im_memory_running_moderate}. */void Ontrimmemory (int level);
Ontrimmemory (int level) does different things depending on levels

Trim_memory_complete:

The system is in a low-memory running state if the system does not have a memory recycle now your app will be the first to be killed. You must release all non-critical resources to restore the status of your app.

Trim_memory_moderate

The system is in a low-memory running state and your app is at the intermediate stage of the Cache app list. If you receive a limit on the memory you are running, your app is at risk of being killed.

Trim_memory_background:

The system is in a low-memory running state and your app is in the early stages of the Cache app list. While your app is not at high risk of being killed, the system is already clearing other apps in the cache list, so you have to free up resources to keep your app in the list so that users can return to your app again and quickly.

Trim_memory_ui_hidden

This process is displayed to the user interface, prompting the application and UI to be released when the memory is large and the UI is not visible

Trim_memory_running_critical

The application is running but the system has already killed most of the cache applications, you must release the resources that are not very critical, and if the system does not reclaim enough running memory, the system clears all cache applications and kills the active application.

Trim_memory_running_low

The application is running and will not be killed, the device can use very low memory, can release some of the unused resources to improve performance (will directly affect the performance of the program)

Trim_memory_running_moderate

The application is in a running state and is not killed, the device uses less memory, and the system level Kill some other cache applications.

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 Strum Onlowmemory () and Ontrimmemory ()

Related Article

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.