文章开始我们首先明白几个问题
- appDiedLocked()这个方法是干什么的?
- 这个方法从那里调用过来,最终要达到什么目的?
- 理解了这个方法对于我们有什么好处?
首先我们得明白appDiedLocked()这个方法是干什么的?这个方法我从ActivityThread.main()中入手,从这里入手的原因,是因为进程创建之后会从这个方法开始绑定AMS,在AMS中通过调用AMS.attachApplicationLocked()
这个方法开始绑定,在这个方法中会有对ApplicationThreadProxy绑定通知,见代码。
private final boolean attachApplicationLocked(IApplicationThread thread,
int pid) {
//绑定死亡通知,此处thread真实数据类型为ApplicationThreadProxy
AppDeathRecipient adr = new AppDeathRecipient(app, pid, thread);
thread.asBinder().linkToDeath(adr, 0);
app.deathRecipient = adr;
}
在这个过程中,当我们binder server挂掉后,会回调AppDeathRecipient.binderDied()
方法通知client端。
那到这里我们的server端是指应用进程的ApplicationThread,其中ApplicationThread是在ActivityThread中创建的此时属于新建的进程(比如新建app的进程)。client就是ApplicationThreadProxy
对象,这个对象是在AMS中,AMS是在system_server中。所以当我们binder server端死亡的时候(app进程死亡)我们system_server进程就会收到通知。做一些处理,我们今天就着重聊聊怎么处理。
1.1 AMS.appDiedLocked()
ActivityManagerService.java
final void appDiedLocked(ProcessRecord app, int pid, IApplicationThread thread,
boolean fromBinderDied) {
//当进程还没有设置已被杀的标记,则进入该分支杀掉相应进程
if (!app.killed) {
//非binder死亡回调,而是上层直接调用该方法,则进入该分支
if (!fromBinderDied) {
Process.killProcessQuiet(pid);//关于这个方法我们后面会单独写文章讲述
}
killProcessGroup(app.info.uid, pid);
app.killed = true;
}
if (app.pid == pid && app.thread != null &&
app.thread.asBinder() == thread.asBinder()) {
//一般为true
boolean doLowMem = app.instrumentationClass == null;
boolean doOomAdj = doLowMem;
boolean homeRestart = false;
if (!app.killedByAm) {
//当app不是由am所杀,则往往都是lmk所杀
if (mHomeProcessName != null && app.processName.equals(mHomeProcessName)) {
mHomeKilled = true;
homeRestart = true;
}
//既然是由lmk所杀,说明当时内存比较紧张,这时希望能被杀
mAllowLowerMemLevel = true;
} else {
mAllowLowerMemLevel = false;
doLowMem = false;
}
//从ams移除该进程以及connections [1.2]
handleAppDiedLocked(app, false, true);
//一般为true,则需要更新各个进程的adj
if (doOomAdj) {
updateOomAdjLocked();//这个方法我们后面会单独写文章总结
}
//当进程是由lmkd所杀,则进入该分支
if (doLowMem) {
//只有当mLruProcesses中所有进程都运行在前台,才报告内存信息
doLowMemReportIfNeededLocked(app);
}
if (mHomeKilled && homeRestart) {
Intent intent = getHomeIntent();
//根据intent解析相应的home activity信息
ActivityInfo aInfo = mStackSupervisor.resolveActivity(intent, null, 0, null, 0);
//当桌面被杀,则立马再次启动桌面进程
startProcessLocked(aInfo.processName, aInfo.applicationInfo, true, 0,
"activity", null, false, false, true);
homeRestart = false;
}
}
}
- 如果应用不是被AMS杀的,就是被lmk了,这个时候只有的那个mLruProcesses中如果进程都在前台,才会打印内存信息。EventLogTags.AM_LOW_MEMORY这个时候打印mLruProcesses.size(),我们可以不关心这个
- handleAppDiedLocked()这个方法我们下面讲,就是要从AMS中移除这个进程的信息以及一些组件信息。
- updateOomAdjLocked()这个是用来调度进程优先级的,比较复杂我们以后单独有一篇文章会研究这个方法。
- startProcessLocked()当桌面被杀的时候会立马启动桌面进程
- ==mLruProcesses是一个通过lru对进程信息的队列,首元素是最近最少使用的进程==
- ==fromBinderDied是用来判断是不是通过正常binder死亡回调调用过来的方法==
1.2 AMS.handleAppDiedLocked()
ActivityManagerService.java
// restarting = false, allowRestart = true
private final void handleAppDiedLocked(ProcessRecord app,
boolean restarting, boolean allowRestart) {
int pid = app.pid;
//清理应用程序servcie,content providers,BroadcastReceiver[1.3]
boolean kept = cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1);
//这里主要看这个进程信息是不是还需要保存,如果不需要保存,并且不需要重启
//则会在lruProcess中去除这个进程信息,并且在ProcessList中移除掉
if (!kept && !restarting) {
removeLruProcessLocked(app);
if (pid > 0) {
ProcessList.remove(pid);
}
}
//清理activity相关信息, 当应用存在可见的activity则返回true [见小节1.4]
boolean hasVisibleActivities = mStackSupervisor.handleAppDiedLocked(app);
app.activities.clear();
...
//当死亡的app存在可见的Activity, 则恢复栈顶第一个非finish的activity
if (!restarting && hasVisibleActivities && !mStackSupervisor.resumeTopActivitiesLocked()) {
//恢复top activity失败,则再次确保有可见的activity
mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
}
}
小节:
- 先通过cleanUpApplicationRecordLocked()清除应用里面有关service, BroadcastReceiver, ContentProvider的记录相关。
- 清理activity相关信息,当应用存在可见的activity则返回true尝试进行恢复
==cleanUpApplicationRecordLocked()方法会打印log:“cleanUpApplicationRecord -- " + app.pid==
那么既然有allowRestart
这个参数,那么什么时候传递进来这个参数为true,什么时候传递进来为false呢?
-
allowRestart = true
- attachApplicationLocked
- startProcessLocked
- appDiedLocked
- removeProcessLocked
killAllBackgroundProcesses 打印reason为:kill all background killPackageProcessesLocked 打印reason为AMS调用forceStopPackageLocked的reason,比较多 processContentProviderPublishTimedOutLocked 打印reason为:timeout publishing content providers
-
allowRestart = false
- handleAppCrashLocked
handleAppCrashLocked removeProcessLocked 打印reason为:crash
- systemReady
removeProcessLocked 打印的reason为:system update done
1.3 AMS.cleanUpApplicationRecordLocked()
ActivityManagerService.java
private final boolean cleanUpApplicationRecordLocked(ProcessRecord app,
boolean restarting, boolean allowRestart, int index) {
mProcessesToGc.remove(app);
mPendingPssProcesses.remove(app);
------------------------清除crashing的弹框------------------------------
//如果存在,则清除crash/anr/wait对话框
if (app.crashDialog != null && !app.forceCrashReport) {
app.crashDialog.dismiss();
app.crashDialog = null;
}
if (app.anrDialog != null) {
app.anrDialog.dismiss();
app.anrDialog = null;
}
if (app.waitDialog != null) {
app.waitDialog.dismiss();
app.waitDialog = null;
}
app.crashing = false;
app.notResponding = false;
------------------------做一些基本的清理工作------------------------------
app.resetPackageList(mProcessStats);//重置包列表
app.unlinkDeathRecipient();//解除死亡回调同志
app.makeInactive(mProcessStats);
app.waitingToKill = null;
app.forcingToForeground = null;
updateProcessForegroundLocked(app, false, false);//将app从前台进程移除
app.foregroundActivities = false;
app.hasShownUi = false;
app.treatLikeActivity = false;
app.hasAboveClient = false;
app.hasClientActivities = false;
------------------------清理service------------------------------
mServices.killServicesLocked(app, allowRestart);
boolean restart = false;
------------------------清理ContentProvider------------------------------
...
for (int i = app.pubProviders.size() - 1; i >= 0; i--) {
//获取该进程已发表的ContentProvider
ContentProviderRecord cpr = app.pubProviders.valueAt(i);
// allowRestart=true,一般地always=false
final boolean always = app.bad || !allowRestart;
//ContentProvider服务端被杀,则client端进程也会被杀
boolean inLaunching = removeDyingProviderLocked(app, cpr, always);
if ((inLaunching || always) && cpr.hasConnectionOrHandle()) {
restart = true; //需要重启
}
cpr.provider = null;
cpr.proc = null;
}
app.pubProviders.clear();
//处理正在启动并且是有client端正在等待的ContentProvider
if (cleanupAppInLaunchingProvidersLocked(app, false)) {
restart = true;
}
//取消已连接的ContentProvider的注册
if (!app.conProviders.isEmpty()) {
for (int i = app.conProviders.size() - 1; i >= 0; i--) {
ContentProviderConnection conn = app.conProviders.get(i);
conn.provider.connections.remove(conn);
stopAssociationLocked(app.uid, app.processName, conn.provider.uid,
conn.provider.name);
}
app.conProviders.clear();
------------------------清理BroadcastReceiver------------------------------
skipCurrentReceiverLocked(app);
// 取消注册的广播接收者
for (int i = app.receivers.size() - 1; i >= 0; i--) {
removeReceiverLocked(app.receivers.valueAt(i));
}
------------------------清理process------------------------------
app.receivers.clear();
for (int i = mPendingProcessChanges.size() - 1; i >= 0; i--) {
ProcessChangeItem item = mPendingProcessChanges.get(i);
if (item.pid == app.pid) {
mPendingProcessChanges.remove(i);
mAvailProcessChanges.add(item);
}
}
mUiHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget();
// 此时为false,不进入该分支
if (restarting) {
return false;
}
if (!app.persistent || app.isolated) {
if (!replacingPid) {
removeProcessNameLocked(app.processName, app.uid);
}
if (mHeavyWeightProcess == app) {
mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG,
mHeavyWeightProcess.userId, 0));
mHeavyWeightProcess = null;
}
} else if (!app.removed) {
//对于persistent应用,则需要重启
// and start a new process for it.
if (mPersistentStartingProcesses.indexOf(app) < 0) {
mPersistentStartingProcesses.add(app);
restart = true;
}
}
//mProcessesOnHold:记录着试图在系统ready之前就启动的进程。
//在那时并不启动这些进程,先记录下来,等系统启动完成则启动这些进程。
mProcessesOnHold.remove(app);
if (app == mHomeProcess) {
mHomeProcessName = mHomeProcess.processName;
mHomeProcess = null;
}
if (app == mPreviousProcess) {
mPreviousProcess = null;
}
if (restart && !app.isolated) {
//仍有组件需要运行在该进程中,因此重启该进程
if (index < 0) {
ProcessList.remove(app.pid);
}
addProcessNameLocked(app);
startProcessLocked(app, "restart", app.processName);
return true;
} else if (app.pid > 0 && app.pid != MY_PID) {
//移除该进程相关信息
boolean removed;
synchronized (mPidsSelfLocked) {
mPidsSelfLocked.remove(app.pid);
mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
}
mBatteryStatsService.noteProcessFinish(app.processName, app.info.uid);
if (app.isolated) {
mBatteryStatsService.removeIsolatedUid(app.uid, app.info.uid);
}
app.setPid(0);
}
}
补充说明:
在Android中有一类应用属于persistent
应用,是一种永久性的应用,他们会在AndroidManifest.xml中将persistent属性设置为true。比如:
<application android:name="PhoneApp"
android:persistent="true"
android:label="@string/dialerIconLabel"
android:icon="@drawable/ic_launcher_phone">
在系统启动之时,AMS的systemReady()会加载所有persistent为true的应用。
public void systemReady(final Runnable goingCallback)
{
List apps = AppGlobals.getPackageManager().getPersistentApplications(STOCK_PM_FLAGS);
if (apps != null) {
int N = apps.size();
int i;
for (i=0; i<N; i++) {
ApplicationInfo info = (ApplicationInfo)apps.get(i);
if (info != null && !info.packageName.equals("android")){
addAppLocked(info, false);
}
}
}
}
2.1 ASS.handleAppDiedLocked()
boolean handleAppDiedLocked(ProcessRecord app) {
//Activity暂停的过程中进程已死则无需走暂停流程
if (mPausingActivity != null && mPausingActivity.app == app) {
mPausingActivity = null;
}
//上次暂停activity,如果运行在该app则也清空
if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
mLastPausedActivity = null;
mLastNoHistoryActivity = null;
}
//[见流程3.1]
return removeHistoryRecordsForAppLocked(app);
}
3.1 AS.handleAppDiedLocked
boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
"mStoppingActivities");
removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
"mGoingToSleepActivities");
removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
"mWaitingVisibleActivities");
removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
"mFinishingActivities");
boolean hasVisibleActivities = false;
int i = numActivities();
for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
final ActivityRecord r = activities.get(activityNdx);
--i;
if (r.app == app) {
//当该activity可见,则设置该标识
if (r.visible) {
hasVisibleActivities = true;
}
final boolean remove;
if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
//当r没有状态 或者正在结束,则需要rmove
remove = true;
} else if (r.launchCount > 2 &&
r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
remove = true;
} else {
remove = false;
}
if (remove) {
if (!r.finishing) {
if (r.state == ActivityState.RESUMED) {
mService.updateUsageStats(r, false);
}
}
} else {
r.app = null;
r.nowVisible = false;
if (!r.haveState) {
r.icicle = null;
}
}
//清理Activity信息
cleanUpActivityLocked(r, true, true);
if (remove) {
//移除Activity
removeActivityFromHistoryLocked(r, "appDied");
}
}
}
}
return hasVisibleActivities;
移除下面关于activity的信息:
- AS.mLRUActivities
- ASS.mStoppingActivities
- ASS.mGoingToSleepActivities
- ASS.mWaitingVisibleActivities
- ASS.mFinishingActivities
总体来说这个就是清理进程相关的信息,但是有很多疑点,就是里面牵扯到很多组件而且判断有没有组件存活,不是进程都杀死了么,怎么还要判断组件存活没???暂时不说了,很晚了,明天继续分析。未完待续...
好了,最后问大神,大神说这里死亡回调最后做清理工作过程中会判断很多东西,比如看是不是存在持久进程,是不是activity在前台,等等问题,所以不单纯是清理工作,而且可能拉起进程。