OnLowMemery与OnTrimMemery以及CompentCallBack

内存优化——系统回调方法OnLowMemory&OnTrimMemory

&ComponentCallbacks

当你把未来交给鸡汤,把梦想交给励志,你以为自己很努力了,到头来都只是自己感动了自己 ——《你所谓的稳定,不过是在浪费生命》

这篇文章关于OnLowMemory&OnTrimMemory&ComponentCallbacks

注意:在官网的阅读的时候,应该更加留意You should,这个就Google的建议,我们应该去做的

1、ComponentCallbacks

ComponentCallbacks:有ComponentCallbacks, 以及ComponentCallbacks2(ComponentCallbacks2是集成ComponentCallbacks的)

注意:(A)ComponentCallbacks包含:
1、void onConfigurationChanged(Configuration var1);
2、void onLowMemory();

(B)在ComponentCallbacks2多加了:void onTrimMemory(int var1); ;还有注意的点是:有很多int类型的常量值,这些就是对应的onTrimMemory回调的参数值(int var1)也是后面说的“level”

Android developer网站地址(需要梯子):

1、ComponentCallback:
https://developer.android.com/reference/android/content/ComponentCallbacks.html
2、ComponentCallback2:
https://developer.android.com/reference/android/content/ComponentCallbacks2.html

翻译:

ComponentCallbacks

1、The set of callback APIs that are common to all application components (Activity, Service, ContentProvider, and Application).

————所有应用程序组件(Activity,Service,ContentProvider和Application)通用的一组回调API。

2、Note: You should also implement the ComponentCallbacks2 interface, which provides the onTrimMemory(int) callback to help your app manage its memory usage more effectively.

——注意:您还应该实现ComponentCallbacks2接口,该接口提供onTrimMemory(int)回调,以帮助您的应用程序更有效地管理其内存使用。

ComponentCallback2

1、Extended ComponentCallbacks interface with a new callback for finer-grained memory management. This interface is available in all application components (Activity, Service, ContentProvider, and Application).

——扩展的ComponentCallbacks接口与一个新的回调以进行更细的内存管理。该界面可用于所有应用程序组件(活动,服务,内容提供者和应用程序)。

2、You should implement onTrimMemory(int) to incrementally release memory based on current system constraints. Using this callback to release your resources helps provide a more responsive system overall, but also directly benefits the user experience for your app by allowing the system to keep your process alive longer. That is, if you don’t trim your resources based on memory levels defined by this callback, the system is more likely to kill your process while it is cached in the least-recently used (LRU) list, thus requiring your app to restart and restore all state when the user returns to it.

——您应该实现onTrimMemory(int),以根据当前的系统限制来逐步释放内存。使用此回调来释放您的资源有助于提供更全面的响应系统,还可以通过允许系统使您的进程更长时间地保持您的应用程序的用户体验。也就是说,如果您不根据此回调定义的内存级别准备资源,当系统缓存在最近最少使用的(LRU(内存管理))列表中时,系统更有可能杀死您的进程,因此要求您的应用程序在用户返回时重新启动并恢复所有状态。

2、OnTrimMemory

——这是在ComponentCallbacks2接口中的方法。“added in API level 14”添加在API14

翻译:

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.

——当操作系统确定是进程从其进程中调整不需要的内存是一个好时机。这将发生在例如在后台进行,并且没有足够的内存来保持尽可能多的后台进程按需运行。您不应该与级别的精确值进行比较,因为可能添加了新的中间值 - 如果值大于或等于您感兴趣的级别,则通常需要进行比较。

2、To retrieve the processes current trim level at any point, you can use activitymanager.getmymemorystate(runningappprocessinfo).

——要在任何时候恢复进程当前的trim级别,可以使用activitymanager.getmymemorystate(runningappprocessinfo).

3、activitymanager.getmymemorystate(runningappprocessinfo).

Return global memory state information for the calling process. This does not fill in all fields of the ActivityManager.RunningAppProcessInfo .The only fields that will be filld in are pid, uid, lastTrimLevel, importance, lru, and importanceReasonCode.

——返回调用进程的全局内存状态信息。这不填写ActivityManager.RunningAppProcessInfo的所有字段。

OnTrimMemory的参数是一个int数值,代表不同的内存状态:

When your app is running:

1、TRIM_MEMORY_RUNNING_MODERATE (中等):
(1)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.(2)The device is beginning to run low on memory. Your app is running and not killable.

——(1)该过程不是一个消耗性的后台进程,但是设备运行的内存不足。您正在运行的进程可能要释放一些不需要的资源,以供其他地方使用。(2)该设备开始运行内存不足。你的应用程序正在运行,不能杀死。
(1)
(1)
(2)
(2)

2、TRIM_MEMORY_RUNNING_LOW :(内存低)
The device is running much lower on memory. Your app is running and not killable, but please release unused resources to improve system performance (which directly impacts your app’s performance).

——该设备的内存运行要低得多。您的应用程序正在运行并且不可杀,但请释放未使用的资源以提高系统性能(直接影响应用程序的性能)。

3、TRIM_MEMORY_RUNNING_CRITICAL (危急)
(1)The device is running extremely low on memory. Your app is not yet considered a killable process, but the system will begin killing background processes if apps do not release resources, so you should release non-critical resources now to prevent performance degradation.(2)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 onLowMemory() called to report that nothing at all can be kept in the background, a situation that can start to notably impact the user.

——(1)设备的内存运行极低。您的应用程序尚未被认为是可杀死的进程,但如果应用程序不释放资源,系统将开始杀死后台进程,因此您现在应该释放非关键资源以防止性能下降。(2)该设备的内存运行极低,并且将无法保持任何后台进程的运行。您的正在运行的进程应该释放尽可能多的非关键资源,以便允许在其他地方使用该内存。接下来要发生的事情是onLowMemory()调用报告,根本就没有什么可以保留在后台,一种可以开始显着影响用户的情况。

总结一句:当应用程序运行时,系统返回回来对设备内存判断的三个级别,中等—>低—->危急

When your app’s visibility changes:

1、TRIM_MEMORY_UI_HIDDEN
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.(2)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.

——该过程已经显示了用户界面,不再这样做。此时应该释放具有UI的大量分配,以便更好地管理内存。(2)该过程已经显示了用户界面,不再这样做。此时应该释放具有UI的大量分配,以便更好地管理内存。

When your app’s process resides in the background LRU list:

1、TRIM_MEMORY_BACKGROUND
The system is running low on memory and your process is near the beginning of the LRU list. Although your app process is not at a high risk of being killed, the system may already be killing processes in the LRU list, so you should release resources that are easy to recover so your process will remain in the list and resume quickly when the user returns to your app.(2)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.

——系统运行内存不足,您的进程接近LRU列表的开头。虽然您的应用进程没有被杀死的高风险,但系统可能已经在LRU列表中杀死进程,所以您应该释放易于恢复的资源,以便您的进程将保留在列表中,并在用户返回到应用程序时快速恢复。(2)该过程已经进入LRU列表。这是清理资源的好机会,如果用户返回到应用程序,可以高效快速地重新构建资源。

2、TRIM_MEMORY_MODERATE
The system is running low on memory and your process is near the middle of the LRU list. If the system becomes further constrained for memory, there’s a chance your process will be killed.(2) 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.

——系统运行内存不足,您的进程靠近LRU列表的中间。如果系统进一步限制内存,那么您的进程有可能会被杀死。(2)该过程是在背景LRU列表的中间附近;释放内存可以帮助系统在列表中稍后运行其他进程,以获得更好的整体性能

3、TRIM_MEMORY_COMPLETEthe process is nearing the end of the background LRU list, and if more memory isn’t found soon it will be killed.(2)The system is running low on memory and your process is one of the first to be killed if the system does not recover memory now. You should release absolutely everything that’s not critical to resuming your app state.
该过程接近背景LRU列表的末尾,并且如果不久之后找到更多的内存将被杀死。(2)系统运行内存不足,如果系统现在不能恢复内存,则进程是第一个被杀死的进程。您应该绝对发布一切对恢复应用程式状态至关重要的内容。

总结一句: ####当App在background LRU list后台内存管理列表的时候,有三个层度:在开头(不容易被杀死)—->中间(可能)——>末尾(即将被杀死)

比如在Activity中就可以直接,调用到OnTrimMemory(int level)的方法
就如官网上面说的一样,可以在“所有组件”中调用。

3、onLowMemory()

This is called when the overall system is running low on memory, and actively running processes should trim their memory usage.
——当整个系统运行内存不足时,这是被调用的,并且主动运行的进程应该调整其内存使用。

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 onTrimMemory(int) from 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 onLowMemory() method as a fallback for older versions, which can be treated the same as onTrimMemory(int) with the TRIM_MEMORY_COMPLETE level.

——最好应该从ComponentCallbacks2实现onTrimMemory(int),以根据各种内存需求逐步卸载资源。
该API可用于API级别14及更高版本,因此您只应将此onLowMemory()方法用作旧版本的后备程序,可以使用TRIM_MEMORY_COMPLETE级别对待onTrimMemory(int)。

总结一句:

  1. 系统会内存不足的时候,回调OnLowMemory方法
  2. 实现OnLowMemory方法来释放您可能持有的任何高速缓存或其他不必要的资源
  3. OnLowMemory相当于OnTrimMemory方式回调的级别(level)为 TRIM_MEMORY_COMPLETE(App在LRU的末尾
  4. 但是由于OnTrimMemory是API 14(Android 4.0)以上的,onLowMemory适用于低版本(P:市面上4.0以下的占有率不多)

2017/10/22 在Android Studio中显示的

4、onConfigurationChanged(Configuration newConfig)

Called by the system when the device configuration changes while your component is running.
——当组件运行时,设备配置发生变化时,系统调用该功能

Called by the system when the device configuration changes while your component is running. Note that, unlike activities, other components are never restarted when a configuration changes: they must always deal with the results of the change, such as by re-retrieving resources.

At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.

——当组件运行时,设备配置发生变化时,系统调用该功能。请注意,与活动不同,其他组件在配置更改时不会重新启动:它们必须始终处理更改的结果,例如重新检索资源。
在调用此函数时,您的Resources对象将被更新,以返回与新配置匹配的资源值。

参数Configuration

This class describes all device configuration information that can impact the resources the application retrieves. This includes both user-specified configuration options (locale list and scaling) as well as device configurations (such as input modes, screen size and screen orientation).

You can acquire this object from Resources, using getConfiguration(). Thus, from an activity, you can get it by chaining the request with getResources():

——此类描述可能影响应用程序检索的资源的所有设备配置信息这包括用户指定的配置选项(区域设置列表和缩放)以及设备配置(如输入模式,屏幕尺寸和屏幕方向)。

您可以使用getConfiguration()从Resources获取此对象。因此,从活动中,您可以通过使用getResources()链接请求来获取它:

感谢大神:

http://www.cnblogs.com/xiajf/p/3993599.html

demo地址:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值