【原創】Android記憶體管理-OnTrimMemory,androidontrimmemory

來源:互聯網
上載者:User

【原創】Android記憶體管理-OnTrimMemory,androidontrimmemory

Application中有兩個與記憶體管理相關的方法:onLowMemory()和 onTrimMemory(int level),源碼如下

  @CallSuper    public void onLowMemory() {        Object[] callbacks = collectComponentCallbacks();        if (callbacks != null) {            for (int i=0; i<callbacks.length; i++) {                ((ComponentCallbacks)callbacks[i]).onLowMemory();            }        }    }    @CallSuper    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);                }            }        }    }

從源碼我們可以看到Application收到這兩個回調時會通知它的監聽者,而Activity和Service都註冊了監聽,

因此我們可以Application中重寫這兩個方法,也可以在組件中重寫這兩個方法。

 

先重點介紹一下onTrimMemory

為了更好的管理記憶體,OnTrimMemory 方法在 API-14 被引入。這個回調可以在所有組件中擷取到(ActivityServiceContentProvider, and Application)。

你應該根據當前裝置的限制複寫 onTrimMemory(int) 來逐步的釋放記憶體。通過複寫這個方法釋放資源可以協助你的app更好的響應系統整體,同時通過讓你的app

在系統中存活更久來提高使用者體驗。如果在系統記憶體很低時,你仍舊不釋放記憶體,系統將會優先殺死你在的進程。這樣當使用者返回app時需要重啟影響使用者體驗

 

onTrimMemory(int)的level值並不成線性關係,它只是提供了記憶體不同狀態的線索。

1. 回調時機

  /**     * 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.     *     * <p>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);

當作業系統認為這是一個進程釋放無用記憶體的好時機時,會調用此方法。比如說當已經沒有足夠的記憶體來維持目前所有的後台進程,而此進程正好處於後台。
非常不推薦用一個精確的值來與level作比較,因為可能會增加新的差值,推薦的做法是判斷一個值是否大於或者等於你感興趣的level.

為了擷取所有進程目前的level,你可以調用{@link android.app.ActivityManager#getMyMemoryState* ActivityManager.getMyMemoryState(RunningAppProcessInfo)}

 

2.level值的具體含義

 

 /**     * 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;

 

當你的app在後台時:

TRIM_MEMORY_COMPLETE :當前進程在LRU列表的尾部,如果沒有足夠的記憶體,它將很快被殺死。這時候你應該釋放任何不影響app啟動並執行資源。

TRIM_MEMORY_MODERATE :當前進程在LRU列表的中部,如果系統進一步需要記憶體,你的進程可能會被殺死。

TRIM_MEMORY_BACKGROUND:當前進程在LRU列表的頭部,雖然你的進程不會被高優殺死,但是系統已經開始準備殺死LRU列表中的其他進程了,

                   因此你應該盡量的釋放能夠快速回複的資源,以保證當使用者返回你的app時可以快速恢複。                   。

 

當你的app的可見度改變時:

TRIM_MEMORY_UI_HIDDEN:當前進程的介面已經不可見,這時是釋放UI相關的資源的好時機。

 

當你的app正在運行時:

TRIM_MEMORY_RUNNING_CRITICAL:雖然你的進程不會被殺死,但是系統已經開始準備殺死其他的後台進程了,這時候你應該釋放無用資源以防止效能下降。

                 下一個階段就是調用"onLowMemory()"來報告開始殺死後台進程了,特別是狀況已經開始影響到使用者。

TRIM_MEMORY_RUNNING_LOW:雖然你的進程不會被殺死,但是系統已經開始準備殺死其他的後台進程了,你應該釋放不必要的資源來提供系統效能,否則會

                 影響使用者體驗。

TRIM_MEMORY_RUNNING_MODERATE:系統已經進入了低記憶體的狀態,你的進程正在運行但是不會被殺死。

 

再來說一下onLowMemory

在引入OnTrimMemory之前都是使用OnLowMemory方法。如果你的app運行在API-14+的機器上,應該使用 OnTrimMemory(int),OnLowMemory的調用時機大概等同於TRIM_MEMORY_COMPLETE.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.