All process state definitions. (In ActivityManager.java)
259 /** @hide Process does not exist. */ 260 public static final int PROCESS_STATE_NONEXISTENT = -1; 261 262 /** @hide Process is a persistent system process. */ 263 public static final int PROCESS_STATE_PERSISTENT = 0; 264 265 /** @hide Process is a persistent system process and is doing UI. */ 266 public static final int PROCESS_STATE_PERSISTENT_UI = 1; 267 268 /** @hide Process is hosting the current top activities. Note that this covers 269 * all activities that are visible to the user. */ 270 public static final int PROCESS_STATE_TOP = 2; 271 272 /** @hide Process is important to the user, and something they are aware of. */ 273 public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 3; 274 275 /** @hide Process is important to the user, but not something they are aware of. */ 276 public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 4; 277 278 /** @hide Process is in the background running a backup/restore operation. */ 279 public static final int PROCESS_STATE_BACKUP = 5; 280 281 /** @hide Process is in the background, but it can't restore its state so we want 282 * to try to avoid killing it. */ 283 public static final int PROCESS_STATE_HEAVY_WEIGHT = 6; 284 285 /** @hide Process is in the background running a service. Unlike oom_adj, this level 286 * is used for both the normal running in background state and the executing 287 * operations state. */ 288 public static final int PROCESS_STATE_SERVICE = 7; 289 290 /** @hide Process is in the background running a receiver. Note that from the 291 * perspective of oom_adj receivers run at a higher foreground level, but for our 292 * prioritization here that is not necessary and putting them below services means 293 * many fewer changes in some process states as they receive broadcasts. */ 294 public static final int PROCESS_STATE_RECEIVER = 8; 295 296 /** @hide Process is in the background but hosts the home activity. */ 297 public static final int PROCESS_STATE_HOME = 9; 298 299 /** @hide Process is in the background but hosts the last shown activity. */ 300 public static final int PROCESS_STATE_LAST_ACTIVITY = 10; 301 302 /** @hide Process is being cached for later use and contains activities. */ 303 public static final int PROCESS_STATE_CACHED_ACTIVITY = 11; 304 305 /** @hide Process is being cached for later use and is a client of another cached 306 * process that contains activities. */ 307 public static final int ROCESS_STATE_CACHED_ACTIVITY_CLIENT = 12; 308 309 /** @hide Process is being cached for later use and is empty. */ 310 public static final int ROCESS_STATE_CACHED_EMPTY = 13;
When it is going to clear the process in PROCESS_STATE_CACHED_ACTIVITY and PROCESS_STATE_CACHED_EMPTY state.
In frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
static final int MAX_CACHED_APPS = SystemProperties.getInt("ro.sys.cached_apps_limit", 24); Get the max number of cached apps process.
int mProcessLimit = ProcessList.MAX_CACHED_APPS;19699 final int emptyProcessLimit; 19700 final int cachedProcessLimit; 19701 if (mProcessLimit <= 0) { 19702 emptyProcessLimit = cachedProcessLimit = 0; 19703 } else if (mProcessLimit == 1) { 19704 emptyProcessLimit = 1; 19705 cachedProcessLimit = 0; 19706 } else { 19707 emptyProcessLimit = ProcessList.computeEmptyProcessLimit(mProcessLimit); 19708 cachedProcessLimit = mProcessLimit - emptyProcessLimit; 19709 }
After this, emptyProcessLimit = cachedProcessLimit = mProcessLimit/2
18360 private final int computeOomAdjLocked(ProcessRecord app, int cachedAdj, ProcessRecord TOP_APP, 18361 boolean doingAll, long now) {
19806 // Count the number of process types. 19807 switch (app.curProcState) { 19808 case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY: 19809 case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT: 19810 mNumCachedHiddenProcs++; 19811 numCached++; 19812 if (numCached > cachedProcessLimit) { If the number of cache app process exceed mProcessLimit/2, then kill the process in PROCESS_STATE_CACHED_ACTIVITY state 19813 app.kill("cached #" + numCached, true); 19814 } 19815 break; 19816 case ActivityManager.PROCESS_STATE_CACHED_EMPTY: 19817 if (numEmpty > ProcessList.TRIM_EMPTY_APPS 19818 && app.lastActivityTime < oldTime) { if a the number of empty process exceed mProcessLimit/4, and be empty for 30 minutes, kill it. (static final long MAX_EMPTY_TIME = 30*60*1000;) 19819 app.kill("empty for " 19820 + ((oldTime + ProcessList.MAX_EMPTY_TIME - app.lastActivityTime) 19821 / 1000) + "s", true); 19822 } else { 19823 numEmpty++; 19824 if (numEmpty > emptyProcessLimit) { If the number of empty app process exceed mProcessLimit/2, then kill it. 19825 app.kill("empty #" + numEmpty, true); 19826 } 19827 } 19828 break; 19829 default: 19830 mNumNonCachedProcs++; 19831 break; 19832 } 19833
Delay of starting service shen there are many services starting at the moment
320 ComponentName startServiceLocked(IApplicationThread caller, Intent service, 321 String resolvedType, int callingPid, int callingUid, int userId) 322 throws TransactionTooLargeException { 369 370 final ServiceMap smap = getServiceMap(r.userId); 371 boolean addToStarting = false; 372 if (!callerFg && r.app == null && mAm.mStartedUsers.get(r.userId) != null) { 373 ProcessRecord proc = mAm.getProcessRecordLocked(r.processName, r.appInfo.uid, false); 374 if (proc == null || proc.curProcState > ActivityManager.PROCESS_STATE_RECEIVER) { 375 // If this is not coming from a foreground caller, then we may want 376 // to delay the start if there are already other background services 377 // that are starting. This is to avoid process start spam when lots 378 // of applications are all handling things like connectivity broadcasts. 379 // We only do this for cached processes, because otherwise an application 380 // can have assumptions about calling startService() for a service to run 381 // in its own process, and for that process to not be killed before the 382 // service is started. This is especially the case for receivers, which 383 // may start a service in onReceive() to do some additional work and have 384 // initialized some global state as part of that. 385 if (DEBUG_DELAYED_SERVICE) Slog.v(TAG, "Potential start delay of " + r + " in " 386 + proc); 387 if (r.delayed) { 388 // This service is already scheduled for a delayed start; just leave 389 // it still waiting. 390 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Continuing to delay: " + r); 391 return r.name; 392 } 393 if (smap.mStartingBackground.size() >= mMaxStartingBackground) { SystemProperties.get("ro.config.max_starting_bg", "0") 394 // Something else is starting, delay! 395 Slog.i(TAG, "Delaying start of: " + r); 396 smap.mDelayedStartList.add(r); 397 r.delayed = true; 398 return r.name; 399 } 400 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Not delaying: " + r); 401 addToStarting = true; 402 } else if (proc.curProcState >= ActivityManager.PROCESS_STATE_SERVICE) { 403 // We slightly loosen when we will enqueue this new service as a background 404 // starting service we are waiting for, to also include processes that are 405 // currently running other services or receivers. 406 addToStarting = true; 407 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Not delaying, but counting as bg: " + r); 408 } else if (DEBUG_DELAYED_STARTS) { 420 } 421 } else if (DEBUG_DELAYED_STARTS) { 430 } 431 432 return startServiceInnerLocked(smap, service, r, callerFg, addToStarting); 433 }
How to judge a process is empty or not
18360 private final int computeOomAdjLocked(ProcessRecord app, int cachedAdj, ProcessRecord TOP_APP, 18361 boolean doingAll, long now) { 18371 18372 if (app.thread == null) { Application process not start or die 18373 app.adjSeq = mAdjSeq; 18374 app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; 18375 app.curProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY; 18376 return (app.curAdj=app.curRawAdj=ProcessList.CACHED_APP_MAX_ADJ); 18377 } 18378 app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; 18379 app.adjSource = null; 18380 app.adjTarget = null; 18381 app.empty = false; 18382 app.cached = false; 18383 18384 final int activitiesSize = app.activities.size(); 18417 18418 // Determine the importance of the process, starting with most 18419 // important to least, and assign an appropriate OOM adjustment. 18420 int adj; 18421 int schedGroup; 18422 int procState; 18423 boolean foregroundActivities = false; 18424 BroadcastQueue queue; 18425 if (app == TOP_APP) { current app is on the top of the screen 18426 // The last app on the list is the foreground app. 18427 adj = ProcessList.FOREGROUND_APP_ADJ; 18428 schedGroup = Process.THREAD_GROUP_DEFAULT; 18429 app.adjType = "top-activity"; 18430 foregroundActivities = true; 18431 procState = ActivityManager.PROCESS_STATE_TOP; 18432 } else if (app.instrumentationClass != null) { test application process 18433 // Don't want to kill running instrumentation. 18434 adj = ProcessList.FOREGROUND_APP_ADJ; 18435 schedGroup = Process.THREAD_GROUP_DEFAULT; 18436 app.adjType = "instrumentation"; 18437 procState = ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND; 18438 } else if ((queue = isReceivingBroadcast(app)) != null) { 18439 // An app that is currently receiving a broadcast also 18440 // counts as being in the foreground for OOM killer purposes. 18441 // It's placed in a sched group based on the nature of the 18442 // broadcast as reflected by which queue it's active in. 18443 adj = ProcessList.FOREGROUND_APP_ADJ; 18444 schedGroup = (queue == mFgBroadcastQueue) 18445 ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE; 18446 app.adjType = "broadcast"; 18447 procState = ActivityManager.PROCESS_STATE_RECEIVER; 18448 } else if (app.executingServices.size() > 0) { 18449 // An app that is currently executing a service callback also 18450 // counts as being in the foreground. 18451 adj = ProcessList.FOREGROUND_APP_ADJ; 18452 schedGroup = app.execServicesFg ? 18453 Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE; 18454 app.adjType = "exec-service"; 18455 procState = ActivityManager.PROCESS_STATE_SERVICE; 18456 //Slog.i(TAG, "EXEC " + (app.execServicesFg ? "FG" : "BG") + ": " + app); 18457 } else { 18458 // As far as we know the process is empty. We may change our mind later. 18459 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; 18460 // At this point we don't actually know the adjustment. Use the cached adj 18461 // value that the caller wants us to. 18462 adj = cachedAdj; 18463 procState = ActivityManager.PROCESS_STATE_CACHED_EMPTY; current process has not activity, services, receivers. 18464 app.cached = true; 18465 app.empty = true; 18466 app.adjType = "cch-empty"; 18467 } 18468 18469 // Examine all activities if not already foreground. 18470 if (!foregroundActivities && activitiesSize > 0) { 18471 for (int j = 0; j < activitiesSize; j++) { 18682 if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { 18683 ProcessRecord client = cr.binding.client; 18684 int clientAdj = computeOomAdjLocked(client, cachedAdj, 18685 TOP_APP, doingAll, now); 18686 int clientProcState = client.curProcState; 18687 if (clientProcState >= ActivityManager.PROCESS_STATE_CACHED_ACTIVITY) { 18688 // If the other app is cached for any reason, for purposes here 18689 // we are going to consider it empty. The specific cached state 18690 // doesn't propagate except under certain conditions. 18691 clientProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY; 18692 } 18767 18768 if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { 18769 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { 18770 schedGroup = Process.THREAD_GROUP_DEFAULT; 18771 } 18772 if (clientProcState <= ActivityManager.PROCESS_STATE_TOP) { 18773 if (clientProcState == ActivityManager.PROCESS_STATE_TOP) { 18774 // Special handling of clients who are in the top state. 18775 // We *may* want to consider this process to be in the 18776 // top state as well, but only if there is not another 18777 // reason for it to be running. Being on the top is a 18778 // special state, meaning you are specifically running 18779 // for the current top app. If the process is already 18780 // running in the background for some other reason, it 18781 // is more important to continue considering it to be 18782 // in the background state. 18783 mayBeTop = true; 18784 clientProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY; 18785 } else { 18786 // Special handling for above-top states (persistent 18787 // processes). These should not bring the current process 18788 // into the top state, since they are not on top. Instead 18789 // give them the best state after that. 18790 clientProcState = 18791 ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND; 18792 } 18793 } 18794 } 18841 18842 for (int provi = app.pubProviders.size()-1; 18843 provi >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ 18844 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE 18845 || procState > ActivityManager.PROCESS_STATE_TOP); 18846 provi--) { 18847 ContentProviderRecord cpr = app.pubProviders.valueAt(provi); 18861 if (clientProcState >= ActivityManager.PROCESS_STATE_CACHED_ACTIVITY) { 18862 // If the other app is cached for any reason, for purposes here 18863 // we are going to consider it empty. 18864 clientProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY; 18865 } 18882 if (clientProcState <= ActivityManager.PROCESS_STATE_TOP) { 18883 if (clientProcState == ActivityManager.PROCESS_STATE_TOP) { 18884 // Special handling of clients who are in the top state. 18885 // We *may* want to consider this process to be in the 18886 // top state as well, but only if there is not another 18887 // reason for it to be running. Being on the top is a 18888 // special state, meaning you are specifically running 18889 // for the current top app. If the process is already 18890 // running in the background for some other reason, it 18891 // is more important to continue considering it to be 18892 // in the background state. 18893 mayBeTop = true; 18894 clientProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY; 18895 }
本文详细介绍了Android系统中进程的不同状态定义及其应用场景,包括如何判断进程是否为空、如何延迟启动服务以及何时清理缓存进程等内容。
2396

被折叠的 条评论
为什么被折叠?



