android service onlowmemory,Android乱弹onLowMemory()和onTrimMemory()

今天看郭哥的LitePal框架的源码,刚打开LitePalApplication里面的源码看到了这样一幕

@Override

public void onLowMemory() {

super.onLowMemory();

mContext = getApplicationContext();

}

不太懂郭哥的意思.之前依稀记得有人说起onLowMemory()和onTrimMemory(),于是乎,我就去查了查源码,这篇博客就来乱弹一下onLowMemory()和onTrimMemory()

首先通过郭哥的那段代码,就看到了,如下部分

public void onLowMemory() {

Object[] callbacks = collectComponentCallbacks();

if (callbacks != null) {

for (int i=0; i

来了个接口回调,继续看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();

我去,这么多英文注释,其实人家的英文注释写的很清楚了,onLowMemory()就是在内存比较紧张时,根据优先级把后台程序杀死时,系统回调他,它用在14之前,14之后就出现了onTrimMemory()

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)是根据级别不同做不同的操作

TRIM_MEMORY_COMPLETE:

系统处于低内存的运行状态中如果系统现在没有内存回收你的应用将会第一个被杀掉. 你必须释放掉所有非关键的资源从而恢复应用的状态.

TRIM_MEMORY_MODERATE

系统处于低内存的运行状态中并且你的应用处于缓存应用列表的中级阶段. 如果系运行内存收到限制, 你的应用有被杀掉的风险.

TRIM_MEMORY_BACKGROUND:

系统处于低内存的运行状态中并且你的应用处于缓存应用列表的初级阶段.  虽然你的应用不会处于被杀的高风险中, 但是系统已经开始清除缓存列表中的其它应用, 所以你必须释放资源使你的应用继续存留在列表中以便用户再次回到你的应用时能快速恢复进行使用.

TRIM_MEMORY_UI_HIDDEN

这个过程显示到用户界面,提示占用内存比较大的应用和ui即将被释放,ui不可见

TRIM_MEMORY_RUNNING_CRITICAL

应用处于运行状态但是系统已经把大多数缓存应用杀掉了, 你必须释放掉不是非常关键的资源, 如果系统不能回收足够的运行内存, 系统会清除所有缓存应用并且会把正在活动的应用杀掉.

TRIM_MEMORY_RUNNING_LOW

应用处于运行状态并且不会被杀掉, 设备可以使用的内存非常低, 可以把不用的资源释放一些提高性能(会直接影响程序的性能)

TRIM_MEMORY_RUNNING_MODERATE

应用处于运行状态并且不会被杀掉, 设备使用的内存比较低, 系统级会杀掉一些其它的缓存应用.

OnLowMemory()和OnTrimMemory()的比较

1,OnLowMemory被回调时,已经没有后台进程;而onTrimMemory被回调时,还有后台进程。

2,OnLowMemory是在最后一个后台进程被杀时调用,一般情况是low memory killer 杀进程后触发;而OnTrimMemory的触发更频繁,每次计算进程优先级时,只要满足条件,都会触发。

3,通过一键清理后,OnLowMemory不会被触发,而OnTrimMemory会被触发一次。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/571085.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

图像滤镜艺术---Oilpaint油画滤镜

图像滤镜艺术---Oilpaint油画滤镜 原文:图像滤镜艺术---Oilpaint油画滤镜Oilpaint油画滤镜 图像油画效果实际上是将图像边缘产生一种朦胧,雾化的效果,同时,将一定的边缘模糊化,这样图像整体上看去像素与像素之间就像雾一样随机呈现…

vs2019创建android,Visual Studio 2019编写的安卓程序,生成失败

下载实例不改动任何能够编译成功,稍加改动无法编译成功。布局代码:xmlns:android"http://schemas.android.com/apk/res/android"android:orientation"vertical"android:layout_width"fill_parent"android:layout_height&q…

android来电事件,android – 来电时没有响铃事件

我不知道问题出在哪里.监听器没有捕获到铃声事件(所以我可以决定接受还是拒绝来电).在清单中是这样的:在主要活动onCreate是这样的:IntentFilter filter new IntentFilter();filter.addAction("android.SipDemo.INCOMING_CALL");callReceiver…

转:数据库收缩

1. 数据库的相关属性 在MS中创建数据库时会为数据库分配初始的大小(如下图:数据库和日志两个文件),随着数据库的使用文件会逐渐增大。数据库文件大小的增加有两种方式: 自动增长:在自动增长中可以设置每次的…

python操作excel常用的方法

读操作模块安装  pip install xlrd 写操作模块安装  pip install xlwt xlrd读操作 1、打开excel   xl xlrd.open_workbook(test.xls) 2、通过索引获取工作表   table xl.sheet()[0] #有多个sheet,获取第一个sheet 3、获取第一行的内容   row table.row…

懒加载与预加载

前端性能优化中图片资源的优化。 1.懒加载(延迟加载) 1.图片进入可视区域之后请求图片资源; 2.对于电商等图片较多,页面很长的业务场景很适用; 3.可以减少无效资源的加载; 4.并发加载的资源过多会阻塞js的加…

8.使用Exists监控ZNode的三大Change事件

一、 zookeeper是一个分布式的协调程序(所有程序都是通过订阅它来相互感知)1. tcp(长链接) watcherserver -》clientclient -》server2. Driver 中的方法 exists() 监控一个znode的 CURD 的操作。client1 client2 同时订阅 baidu…

广东 职称英语计算机,现在评职称英语和计算机都不用考了

技校网专门为您推荐的类似问题答案问题1:通过出版职业资格考试中级考试,评职称需要职称英语和计算机条件不跟随 已跟随 取消 确定 1、要看职称委员会的要求而定。 2、看你中级报的是什么中级?如是工程师,就要参加全国的职称英语和计算机考试&…

[SDOI2016]储能表

Description 有一个 n 行 m 列的表格,行从 0 到 n−1 编号,列从 0 到 m−1 编号。每个格子都储存着能量。最初,第 i 行第 j 列的格子储存着 (i xor j) 点能量。所以,整个表格储存的总能量是, 随着时间的推移&#xff0…

军用计算机ip等级是什么,加固计算机的IP防护等级是什么?

加固计算机的IP防护等级是什么?作者:鲁成伟业 | 时间:2021-04-28 09:00:43 | 人已围观加固计算机是各种现代化w器z备系统、j事电子信息系统以及国防科研系统中不可缺少的、到处可见的设备。小到嵌入式的微处理器,大到百万亿次级的…

SpringMvc渲染视图

这篇博文讨论的问题是从ModelAndView如何渲染到页面。 首先要知道每个请求处理完之后都会返回一个ModelAndView对象。 这里我分6种情况来分析,代表6种返回类型: ModelAndViewMap,ModelMapModelViewStringVoid我先贴出我的测试的后台代码&…

山西计算机工程师职称英语,山西中级工程师职称在线查询

技校网专门为您推荐的类似问题答案问题1:新疆石河子中级工程师职称在哪查询应该还没出,这里说不定有吧。。。不确定~问题2:武汉市中级工程师职称如何办理楼主,看你是要办那个对方的,现在大多数市面上都是随州办理的 比较容易办到 还有专科毕业…

spring3: 切面及通知实例 Aspectj的aop

1.前置通知 接口: package chapter1.server;public interface IHelloService {public void sayAdvisorBefore(String param) ; }实现 package chapter1.service.impl;import chapter1.server.IHelloService;public class HelloService implements IHelloService { p…

微型计算机接口与技术期末,微机原理与接口技术期末考试试题及答案.pdf

微机原理与接口技术期末考试题库1. 微机系统的硬件由哪几部分组成?答:三部分:微型计算机 (微处理器,存储器,I/0接口,系统总线),外围设备,电源。2. 什么是微机的总线,分为…

html对图片轮播脚本怎么调用,【jquery前端开发】可调整的幻灯片(图片轮播)

第一次写博客,希望接下来写的东西 或多或少能帮到些人,虽然这些东西都是一些大神前辈们写了无数遍的东西,但我尽量以一名小白的视角把代码写得清楚点,好心人的就给点赞吧。1.前期准备这是我们编写代码前必须要做的事,在…

数据类型总结(干货)

Java中的数据类型和C的数据类型基本是一致的,本来以为不需要怎么看,后来发现还是有些地方需要好好总结一下。 基本的就不说了,直接上干货。 我总结了下,数据类型的转换和赋值有以下几点是比较麻烦的: 1、整数直接量可以…

计算机主机信息怎么看,本机电脑硬件配置信息怎么看?Win7/Win10查看详细电脑配置方法...

电脑配置决定了一台电脑的性能好坏,如果电脑配置没有达到游戏或者软件的要求,那么肯定无法流畅运行的。对于一些小白用户不知道如何查看电脑硬件配置,那么本机电脑硬件配置信息怎么看?下面装机之家小编分享一下Win7/Win10查看详细…

软件工程网络15个人阅读作业2(201521123024丁树乐)

提出问题 question1.我看到书P27中写到“100%的代码覆盖率并不等同于100%的正确性”那么怎么样去确保100%的正确性? 写代码注意结构和代码规范,注释要写全,代码尽量精简,不要一坨坨的堆在一起。 在目前行业内的成熟方案有如下几个…

Uva 1025 - A Spy in the Metro(DP)

题目链接 https://vjudge.net/problem/UVA-1025 【题意】 某城市里的地铁是线性的&#xff0c;有n个车站&#xff08;2<n<50&#xff09;&#xff0c;有M1辆列车从第1站从左往右开&#xff0c;有M2辆列车从第n站从右往左开&#xff0c;在0时刻间谍从第一站出发&a…

未来计算机控制器趋势,未来DCS控制系统技术发展4大趋势

DCS系统是继PLC之外的一大自动化控制系统&#xff0c;它在化工、火电等领域的应用极为广泛&#xff0c;但是生产方面的自动化技术需求进一步提高&#xff0c;传统的DCS系统已不能满足需要&#xff0c;需要进行技术升级。DCS系统由多台计算机分别控制生产过程中多个控制回路&…