android ontrimmemory

为了更好的管理内存,OnTrimMemory 方法在 API-14 被引入

 

 void onTrimMemory(int 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.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值