2019独角兽企业重金招聘Python工程师标准>>>

OnLowMemory

OnLowMemory是Android提供的API,在系统内存不足,所有后台程序(优先级为background的进程,不是指后台运行的进程)都被杀死时,系统会调用OnLowMemory。系统提供的回调有:

  • Application.onLowMemory()

  • Activity.OnLowMemory()

  • Fragement.OnLowMemory()

  • Service.OnLowMemory()

  • ContentProvider.OnLowMemory()

除了上述系统提供的API,还可以自己实现ComponentCallbacks,通过API注册,这样也能得到OnLowMemory回调。例如:

public static class MyCallback implements ComponentCallbacks {         @Override        public void onConfigurationChanged(Configuration arg) {         }

        @Override        public void onLowMemory() {            //do release operation        }    }

然后,通过Context.registerComponentCallbacks ()在合适的时候注册回调就可以了。通过这种自定义的方法,可以在很多地方注册回调,而不需要局限于系统提供的组件。

onLowMemory 当后台程序已经终止资源还匮乏时会调用这个方法。好的应用程序一般会在这个方法里面释放一些不必要的资源来应付当后台程序已经终止,前台应用程序内存还不够时的情况。

OnTrimMemory

OnTrimMemory是Android 4.0之后提供的API,系统会根据不同的内存状态来回调。系统提供的回调有:

  • Application.onTrimMemory()

  • Activity.onTrimMemory()

  • Fragement.OnTrimMemory()

  • Service.onTrimMemory()

  • ContentProvider.OnTrimMemory()

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

  • TRIM_MEMORY_COMPLETE:内存不足,并且该进程在后台进程列表最后一个,马上就要被清理

  • TRIM_MEMORY_MODERATE:内存不足,并且该进程在后台进程列表的中部。

  • TRIM_MEMORY_BACKGROUND:内存不足,并且该进程是后台进程。

  • TRIM_MEMORY_UI_HIDDEN:内存不足,并且该进程的UI已经不可见了。

以上4个是4.0增加

  • TRIM_MEMORY_RUNNING_CRITICAL:内存不足(后台进程不足3个),并且该进程优先级比较高,需要清理内存

  • TRIM_MEMORY_RUNNING_LOW:内存不足(后台进程不足5个),并且该进程优先级比较高,需要清理内存

  • TRIM_MEMORY_RUNNING_MODERATE:内存不足(后台进程超过5个),并且该进程优先级比较高,需要清理内存

以上3个是4.1增加

系统也提供了一个ComponentCallbacks2,通过Context.registerComponentCallbacks()注册后,就会被系统回调到。

以上int值对应关系:

http://developer.android.com/reference/android/content/ComponentCallbacks2.htmlConstants
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_BACKGROUNDAdded in API level 14Level for 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.Constant Value: 40 (0x00000028)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_COMPLETE Added in API level 14 Level for 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. Constant Value: 80 (0x00000050)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_MODERATE Added in API level 14 Level for 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. Constant Value: 60 (0x0000003c)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_RUNNING_CRITICAL Added in API level 16 Level for 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 onLowMemory() called to report that nothing at all can be kept in the background, a situation that can start to notably impact the user. Constant Value: 15 (0x0000000f)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_RUNNING_LOW Added in API level 16 Level for 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. Constant Value: 10 (0x0000000a)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_RUNNING_MODERATE Added in API level 16 Level for 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. Constant Value: 5 (0x00000005)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_UI_HIDDEN Added in API level 14Level for 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.Constant Value: 20 (0x00000014)

OnLowMemory和OnTrimMemory的比较

1,OnLowMemory被回调时,已经没有后台进程;而onTrimMemory被回调时,还有后台进程。
2,OnLowMemory是在最后一个后台进程被杀时调用,一般情况是low memory killer 杀进程后触发;而OnTrimMemory的触发更频繁,每次计算进程优先级时,只要满足条件,都会触发。
3,通过一键清理后,OnLowMemory不会被触发,而OnTrimMemory会被触发一次。

使用举例:

 1 @Override 2 public void onTrimMemory(int level) { 3     Log.e(TAG, " onTrimMemory ... level:" + level);      6 } 7  8 @Override 9 public void onLowMemory() {     11     Log.e(TAG, " onLowMemory ... ");     13 }

通过 ActivityManager查看进程的内存:

 1 private void displayBriefMemory() { 2     final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 3     ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo(); 4     activityManager.getMemoryInfo(info); 5     Log.i(TAG, "系统剩余内存:" + (info.availMem >> 10) + "k"); 6     Log.i(TAG, "系统是否处于低内存运行:" + info.lowMemory); 7     Log.i(TAG, "当系统剩余内存低于" + (info.threshold >> 10) + "k" + "时就看成低内存运行"); 8  9     util.SavedToText(TAG, "meminfo  系统剩余内存:" + (info.availMem >> 10) + "k"10             + "  " + "系统是否处于低内存运行:" + info.lowMemory + "  " + "当系统剩余内存低于"11             + (info.threshold >> 10) + "k" + "时就看成低内存运行");12 }

转载于:https://my.oschina.net/u/586684/blog/207839

Android 内存相关 onTrimMemory,onLowMemory相关推荐

  1. Android内存管理-OnTrimMemory

    Application中有两个与内存管理相关的方法:onLowMemory()和 onTrimMemory(int level),源码如下 1 @CallSuper 2 public void onL ...

  2. Android内存相关

    预备知识 页: 我们把磁盘分很多块,我们把这个块称为页(Memory paging),页同时也是硬盘和内存最小的交换单位(一般为4k). 如果你想了解更多请参阅wikipedia: Memory pa ...

  3. 关于Android内存优化

    介绍 在Android系统中,内存分配与释放分配在一定程度上会影响App性能的-鉴于其使用的是类似于Java的GC回收机制,因此系统会以消耗一定的效率为代价,进行垃圾回收. 在中国有句老话:" ...

  4. android 内核内存管理,Android内核相关内容总结

    要想充分掌握Android这一操作系统的应用,首先需要我们从Android内核的相关内容开始了解.在这里就为大家详细介绍一下相关的知识. Android操作系统是由谷歌推出的一款基于Linux平台开源 ...

  5. 深入探索Android内存优化

    前言 成为一名优秀的Android开发,需要一份完备的知识体系,在这里,让我们一起成长为自己所想的那样~. 本篇是Android内存优化的进阶篇,难度会比较大,建议对内存优化不是非常熟悉的前仔细看看在 ...

  6. 深入探索Android内存优化(炼狱级别)

    本文由 jsonchao投稿 微信:bcce5360 前言 成为一名优秀的Android开发,需要一份完备的知识体系,在这里,让我们一起成长为自己所想的那样~. 本篇是 Android 内存优化的进阶 ...

  7. 深入探索 Android 内存优化(炼狱级别)

    前言 成为一名优秀的Android开发,需要一份完备的知识体系,在这里,让我们一起成长为自己所想的那样~. 本篇是 Android 内存优化的进阶篇,难度可以说达到了炼狱级别,建议对内存优化不是非常熟 ...

  8. Android内存管理机制官方详解文档

    很早之前写过一篇<Android内存管理机制详解>点击量已7万+,现把Google官方文档整理输出一下,供各位参考. 一.内存管理概览 Android 运行时 (ART) 和 Dalvik ...

  9. Android内存优化的知识梳理

    JVM内存管理基础知识 了解JVM内存管理的基础内容,对我们理解内存分配有很大的帮助:比如Java堆的原理,JVM如何判断对象的存活.几种垃圾回收算法: 关于这部分,可以参考笔者之前写的JVM|翻越内 ...

  10. 这些Android内存管理知识你都知道吗

    谭嘉俊的博客地址: https://juejin.im/user/593f7b33fe88c2006a37eb9b Android Runtime(ART)虚拟机和Dalvik虚拟机都使用分页(Pag ...

最新文章

  1. 下一个大计算平台? Amazon Echo 研发幕后全揭露
  2. Catalyst3550交换机配置三层接口
  3. Java8 stream filter map
  4. 关于类的非静态函数指针成员变量
  5. .net 连接php,NetBeans平台如何连接到PHP解析器?
  6. 单E1光端机,V.35光端机,以太网光端机介绍及技术指标详解
  7. 【强化学习】Policy Gradients代码注释版本
  8. Eclipse+MyEclipse+Tomcat下配置建立Web Project
  9. 布谷鸟哈希函数的参数_系统学习hash算法(哈希算法)
  10. 网站运营之比较和差异化
  11. 简要说明python的缩进规则_关于python的缩进规则的知识点详解
  12. 身体排毒,自己就可以轻松搞定 - 生活至上,美容至尚!
  13. ue4导入abc文件问题
  14. 混合溶剂中的高分子凝胶中的渗透压的一般计算
  15. halcon二维码识别
  16. 你想学习吗?你会学习吗?你知道该如何学习吗?学习之道-读书笔记
  17. 支付宝手机网站支付实战踩坑
  18. After Effects CC SDK 使用指南(二)—— 第一章 介绍 (上)
  19. Python零基础速成班-第9讲-Python面向对象编程(上),对象和类、初始化、继承、重写、多态、类方法、组合
  20. 程序员应对35岁中年危机的措施

热门文章

  1. MUSBMHDRC USB 2.0 MULTI-POINT DUAL-ROLE CONTROLLER编程指南解读3
  2. 利用python实现决策树图片绘制
  3. 为什么python如此火爆_用Python来分析一波周董新曲《说好不哭》为何如此火爆!...
  4. arduino 读取EC11编码器
  5. 量子计算机代表人类最强科技,量子计算机和可控核聚变,谁对人类未来发展更重要?...
  6. 【渝粤题库】国家开放大学2021春3930事故管理与应急处置题目
  7. MySQL学习系列5:函数
  8. 29_互联网(The Internet)(IP数据包;UDP;TCP;DNS;OSI)
  9. 离婚后的30天(转帖)
  10. Burp Suite Pro Loader Keygen