Android Play onLowMemory () and onTrimMemory ()

Source: Internet
Author: User

Android Play onLowMemory () and onTrimMemory ()

Today, I checked the LitePal framework source code of Guo GE. I just opened the LitePalApplication source code and saw this scene.

@Overridepublic void onLowMemory() {super.onLowMemory();mContext = getApplicationContext();}

I don't know much about Guo ge. I vaguely remember someone talking about onLowMemory () and onTrimMemory (). So I checked the source code. This blog will pop up onLowMemory () and onTrimMemory ()


First, you can see through the Code provided by Guo Ge, as shown in the following section:

Public void onLowMemory () {Object [] callbacks = collectComponentCallbacks (); if (callbacks! = Null) {for (int I = 0; I
 
  
An interface callback is provided to continue reading onLowMemory ()

/**     * This is called when the overall system is running low on memory, and     * actively running processes should trim their memory usage.  While     * the exact point at which this will be called is not defined, generally     * it will happen when all background process have been killed.     * That is, before reaching the point of killing processes hosting     * service and foreground UI that we would like to avoid killing.     *     * 

You should implement this method to release * any caches or other unnecessary resources you may be holding on to. * The system will perform a garbage collection for you after returning from this method. *

Preferably, you should implement {@link ComponentCallbacks2#onTrimMemory} from * {@link ComponentCallbacks2} to incrementally unload your resources based on various * levels of memory demands. That API is available for API level 14 and higher, so you should * only use this {@link #onLowMemory} method as a fallback for older versions, which can be * treated the same as {@link ComponentCallbacks2#onTrimMemory} with the {@link * ComponentCallbacks2#TRIM_MEMORY_COMPLETE} level.

*/ void onLowMemory();

I'm going, so many English comments are actually clearly written. onLowMemory () is when the background program is killed according to the priority when the memory is tight, the onTrimMemory () occurs after 14 and before 14 ()

Public void onTrimMemory (int level) {Object [] callbacks = collectComponentCallbacks (); if (callbacks! = Null) {for (int I = 0; I
   
    


/**     * Level for {@link #onTrimMemory(int)}: the process is nearing the end     * of the background LRU list, and if more memory isn't found soon it will     * 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     * other processes running later in the list for better overall performance.     */    static final int TRIM_MEMORY_MODERATE = 60;        /**     * Level for {@link #onTrimMemory(int)}: the process has gone on to the     * LRU list.  This is a good opportunity to clean up resources that 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 doing so.  Large allocations with     * the UI should be 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 is not an expendable     * background process, but the device is running extremely low on memory     * and is about to not be able to keep any background processes running.     * Your running process should free up as many non-critical resources as it     * can to allow that memory to be used elsewhere.  The next thing that     * will happen after this is {@link #onLowMemory()} called to report that     * nothing at all can 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 is not an 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 is not an 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 has determined that it is a good     * time for a process to trim unneeded memory from its process.  This will     * happen for example when it goes in the background and there is not enough     * memory to keep as many background processes running as desired.  You     * should never compare to exact values of the level, since new intermediate     * values may be added -- you will typically want to compare if the value     * is greater or equal to a level you are interested in.     *     * 

To retrieve the processes current trim level at any point, you can * use {@link android.app.ActivityManager#getMyMemoryState * ActivityManager.getMyMemoryState(RunningAppProcessInfo)}. * * @param level The context of the trim, giving a hint of the amount of * trimming the application may like to perform. 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 #TRIM_MEMORY_RUNNING_MODERATE}. */ void onTrimMemory(int level);

OnTrimMemory (int level) performs different operations based on different levels.

TRIM_MEMORY_COMPLETE:

The system is in a low-memory running state. If the system does not have memory to recycle your application, it will be the first to be killed. You must release all non-critical resources to restore the application.

TRIM_MEMORY_MODERATE

The system is in a low-memory running state and your application is in the intermediate stage of the cache application list. If the system runs the memory and receives the limit, your application may be killed.

TRIM_MEMORY_BACKGROUND:

The system is running in low memory and your application is in the initial stage of caching the Application List. although your application is not at high risk of being killed, the system has begun to clear other applications in the cache list, therefore, you must release the resources so that your application remains in the list so that the user can quickly restore to use your application again.

TRIM_MEMORY_UI_HIDDEN

This process is displayed on the user interface, prompting that applications and UIs that occupy a large amount of memory will be released soon, And the ui will not be visible.

TRIM_MEMORY_RUNNING_CRITICAL

The application is running, but the system has killed most cached applications. You must release resources that are not critical. If the system cannot recycle enough running memory, the system will clear all cached applications and kill active applications.

TRIM_MEMORY_RUNNING_LOW

The application is running and will not be killed. The memory available to the device is very low, and unused resources can be released to Improve the Performance (which will directly affect the performance of the program)

TRIM_MEMORY_RUNNING_MODERATE

The application is running and will not be killed. The memory used by the device is relatively low, and the system level will kill some other cache applications.

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.



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.