android在启动进程后会调用attachApplicationLocked,在其中利用Binder的linkToDeath方法来接收进程退出消息

ams中的attachApplicationLocked方法

privatefinal boolean attachApplicationLocked(IApplicationThread thread,int pid) {

...

if (localLOGV) Slog.v(

TAG, "Bindingprocess pid " + pid + " to record " + app);

final StringprocessName = app.processName;

try {

AppDeathRecipient adr = new AppDeathRecipient(

app,pid, thread);

thread.asBinder().linkToDeath(adr, 0);

app.deathRecipient = adr;

} catch(RemoteException e) {

app.resetPackageList(mProcessStats);

startProcessLocked(app, "link fail", processName);

return false;

}

...

}

官方文档中对linkToDeath 的解释

publicabstract void linkToDeath (IBinder.DeathRecipient recipient,int flags)

Registerthe recipient for a notification if this binder goes away. If this binderobject unexpectedly goes away (typically because its hosting process has beenkilled), then the given IBinder.DeathRecipient's DeathRecipient.binderDied() methodwill be called.

You willonly receive death notifications for remote binders, as local binders bydefinition can't die without you dying as well.

就是说我们通过systemService申请了某个service的Binder后,可以调用这个IBinder的linkToDeath函数进行注册。可以注册一个IBinder.DeathRecipient类型的对象。其中IBinder.DeathRecipient是IBinder类中定义的一个嵌入类。当这个IBinder所对应的Service进程被异常的退出时,比如被kill掉,这时系统会调用这个IBinder之前通过linkToDeath注册的DeathRecipient类对象的binderDied函数。

一般实现中,Bp端会注册linkToDeath,目的是为了监听绑定的Service的异常退出,一般的binderDied函数的实现是用来释放一些相关的资源。

private final class AppDeathRecipientimplements IBinder.DeathRecipient {

final ProcessRecord mApp;

final int mPid;

final IApplicationThread mAppThread;

AppDeathRecipient(ProcessRecord app,int pid,

IApplicationThread thread) {

if (localLOGV) Slog.v(

TAG, "New death recipient" + this

+ " for thread " +thread.asBinder());

mApp = app;

mPid = pid;

mAppThread = thread;

}

@Override

public void binderDied() {

if (localLOGV) Slog.v(

TAG, "Death received in" + this

+ " for thread " +mAppThread.asBinder());

synchronized(ActivityManagerService.this) {

appDiedLocked(mApp, mPid,mAppThread);

}

}

}

final void appDiedLocked(ProcessRecord app,int pid,IApplicationThread thread) {

...

if (!app.killedByAm) {

Slog.i(TAG, "Process " + app.processName + " (pid "+ pid

+") has died.");

mAllowLowerMemLevel = true;

}else{

// Note that we always want to do oom adj to update our statewith the

// new number ofprocs.

mAllowLowerMemLevel =false;

doLowMem = false;

}

...

}

到这里应该明白,logcat打印:I/ActivityManager( 510): Process xxx (pid xxx) has died.的时候,IBinder所对应的Service进程异常退出(被杀)造成的。

那么service被杀除了service本身代码逻辑的问题,很多情况就是系统内存不足时引起的,这就又回到内存管理了,这里就简单叙述一下。

Android基于进程中运行的组件及其状态规定了默认的五个回收优先级:

IMPORTANCE_FOREGROUND:

IMPORTANCE_VISIBLE:

IMPORTANCE_SERVICE:

IMPORTANCE_BACKGROUND:

IMPORTANCE_EMPTY:

这几种优先级的回收顺序是 Empty process、Background process、Serviceprocess、Visible process、Foreground process。

ActivityManagerService和ActivityStackSupervisor中涉及到内存回收的几个

重要的成员方法如下:trimApplications(),updateOomAdjLocked() ,activityIdleInternal()。这几个成员方法主要负责Android 默认的内存回收机制。

回收动作入口:activityIdleInternal()

Android系统中内存回收的触发点大致可分为三种情况。第一,用户程序调用 StartActivity(), 使当前活动的 Activity 被覆盖;

第二,用户按 back 键,退出当前应用程序;第三,启动一个新的应用程序。这些能够触发内存回收的事件最终调用的函数接口就是 activityIdleInternal()。

当 ActivityStackSupervisor接收到异步消息IDLE_TIMEOUT_MSG 或者 IDLE_NOW_MSG 时,activityIdleInternal() 将会被调用。

从Process xxxx (pid xxx) has died分析相关推荐

  1. Android程序运行报错Process xxxx has died原因查找

    在我们的程序挂掉之后,我们抓取log分析,有时候有以下提示: "I/ActivityManager( 2212): Process com.seven.test (pid 2758) has ...

  2. Frida出现process with pid XXXX either refused to load frida-agent, or terminated during injection错误的原因

    刚刚Frida出现了一个报错,类似:process with pid 17003 either refused to load frida-agent, or terminated during in ...

  3. child pid xxx exit signal Segmentation fault (11)

    下午检查apahe的error_log的时候发现大量: [Tue Jul 29 14:35:39 2014] [notice] child pid 22397 exit signal Segmenta ...

  4. the android emulator process,Android studio报错:The emulator process for AVD (xxx) was killed

    背景:重做系统后重新配置Android studio 安装虚拟机后无法启动 log中显示为启动AVD的进程被杀 控制台显示为:在虚拟机列表里没有找到所要启动的AVD的名称 首先考虑是BIOS问题:发现 ...

  5. Mac运行Android模拟器报The emulator process for AVD XXX has terminated错误

    最近升级Android Studio之后,运行Android模拟器,一直报错:The emulator process for AVD XXX has terminated. 报错原因:Mac版本过低 ...

  6. Port 80 in use by “Unable to open process“ with PID 4!

    打开apache的时候出现这个错误:Port 80 in use by "Unable to open process" with PID 4! 错误原因:端口80被占用 解决方法 ...

  7. PLC应用关于自动化控制中离散PID模型的理论分析

    1.序言 工业自动化现场,恒压供水,恒温控制,恒转速等技术要求的应用经常会碰到,在自动控制理论中,要实现这些技术,必须要用到的技术就是闭环控制.闭环控制是根据控制对象输出反馈来进行校正的控制方式,它是 ...

  8. fatal: http://gitlab.xxxx.com/xxx/songjy-coocaa.git/info/refs not valid: is this a git repository

    错误信息如下: fatal: http://gitlab.xxxx.com/xxx/songjy-coocaa.git/info/refs not valid: is this a git repos ...

  9. linux删除用户失败:userdel:user xxx is currently used by process xxxx

    问题: 在linux系统中,使用su root命令从其它用户切换到root用户后,使用userdel命令删除用户时,显示userdel:user xxx is currently used by pr ...

最新文章

  1. 15级团队学习成果汇报 -- 利用C#语言实现计算器
  2. Nginx 的内置变量
  3. 利用 CSS selector 改变悬停表格样式
  4. 聚簇索引与非聚簇索引学习总结
  5. java 拼音模糊查询_java实现首字母模糊查询的功能
  6. 163相册密码破解 - 简单版
  7. 初创企业融资应量力而行
  8. QQ音乐2009去广告方法
  9. 便宜又大碗!AI将画廊轻松搬到自家墙壁;用隐写术在图像中存储文件;免费书·算法高维鲁棒统计;关节式手部模型数据集;前沿论文 | ShowMeAI资讯日报
  10. Excel如何统计多种分隔符号的单元格姓名个数
  11. 《A Study on Performance Benefifits of Core Morphing in an Asymmetric Multicore Processor》文献精读1
  12. 了解一下nested数据类型
  13. Go Slice实现原理分析
  14. 《Centos的下载详细步骤》
  15. android安装并启用新输入法
  16. Brain:临床前和早期阿尔茨海默病的睡眠和纵向认知表现
  17. Technical support(技术支持)
  18. SpringCloud 学习笔记系列03--Hystrix熔断器
  19. 测试员≠“背锅侠”:学会这些,扔掉测试人常背的3口“锅”
  20. vba 保存word里面的图片_word表格技巧:如何对表格进行样式批处理

热门文章

  1. 桌面图标不见了,突然消失了,怎么办?如何找回?
  2. 细数ST-LINK的种种坑:ST-LINK Connection error,USB communication error,NO target connected等
  3. opencv-python图像处理 ----边界矩形、外接圆、模板匹配
  4. OSChina 周三乱弹 ——国足要出线,半个亚洲都要帮忙
  5. win7英文版中文显示乱码-很多软件无法安装,都可依此解决
  6. easyx图形库-----贴图技巧之双缓冲消除闪屏(BeginBatchDraw 与 EndBatchDraw)
  7. 从源码角度看for循环和foreach的区别
  8. Vue项目与Nginx反向代理
  9. Ubuntu20.4搭建git服务器
  10. 微信公众号开发C#系列-3、搭建微信本地调试环境-借助花生壳实现内网穿透