ProcessRecord state

来源:互联网 发布:农行网银助手mac 编辑:程序博客网 时间:2024/06/05 06:22

All process state definitions. (In ActivityManager.java)

259    /** @hide Process does not exist. */260    public static final int PROCESS_STATE_NONEXISTENT = -1;261262    /** @hide Process is a persistent system process. */263    public static final int PROCESS_STATE_PERSISTENT = 0;264265    /** @hide Process is a persistent system process and is doing UI. */266    public static final int PROCESS_STATE_PERSISTENT_UI = 1;267268    /** @hide Process is hosting the current top activities.  Note that this covers269     * all activities that are visible to the user. */270    public static final int PROCESS_STATE_TOP = 2;271272    /** @hide Process is important to the user, and something they are aware of. */273    public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 3;274275    /** @hide Process is important to the user, but not something they are aware of. */276    public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 4;277278    /** @hide Process is in the background running a backup/restore operation. */279    public static final int PROCESS_STATE_BACKUP = 5;280281    /** @hide Process is in the background, but it can't restore its state so we want282     * to try to avoid killing it. */283    public static final int PROCESS_STATE_HEAVY_WEIGHT = 6;284285    /** @hide Process is in the background running a service.  Unlike oom_adj, this level286     * is used for both the normal running in background state and the executing287     * operations state. */288    public static final int PROCESS_STATE_SERVICE = 7;289290    /** @hide Process is in the background running a receiver.   Note that from the291     * perspective of oom_adj receivers run at a higher foreground level, but for our292     * prioritization here that is not necessary and putting them below services means293     * many fewer changes in some process states as they receive broadcasts. */294    public static final int PROCESS_STATE_RECEIVER = 8;295296    /** @hide Process is in the background but hosts the home activity. */297    public static final int PROCESS_STATE_HOME = 9;298299    /** @hide Process is in the background but hosts the last shown activity. */300    public static final int PROCESS_STATE_LAST_ACTIVITY = 10;301302    /** @hide Process is being cached for later use and contains activities. */303    public static final int PROCESS_STATE_CACHED_ACTIVITY = 11;304305    /** @hide Process is being cached for later use and is a client of another cached306     * process that contains activities. */307    public static final int ROCESS_STATE_CACHED_ACTIVITY_CLIENT = 12;308309    /** @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 inPROCESS_STATE_CACHED_ACTIVITY andPROCESS_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 state19813                            app.kill("cached #" + numCached, true);19814                        }19815                        break;19816                    case ActivityManager.PROCESS_STATE_CACHED_EMPTY:19817                        if (numEmpty > ProcessList.TRIM_EMPTY_APPS19818                                && 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 {369370        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 want376                // to delay the start if there are already other background services377                // that are starting.  This is to avoid process start spam when lots378                // of applications are all handling things like connectivity broadcasts.379                // We only do this for cached processes, because otherwise an application380                // can have assumptions about calling startService() for a service to run381                // in its own process, and for that process to not be killed before the382                // service is started.  This is especially the case for receivers, which383                // may start a service in onReceive() to do some additional work and have384                // 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 leave389                    // 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 background404                // starting service we are waiting for, to also include processes that are405                // 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        }431432        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) {1837118372        if (app.thread == null) {     Application process not start or die18373            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;1838318384        final int activitiesSize = app.activities.size();1841718418        // Determine the importance of the process, starting with most18419        // 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 screen18426            // 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 process18433            // 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 also18440            // counts as being in the foreground for OOM killer purposes.18441            // It's placed in a sched group based on the nature of the18442            // 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 also18450            // 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 adj18461            // 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        }1846818469        // 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 here18689                            // we are going to consider it empty.  The specific cached state18690                            // doesn't propagate except under certain conditions.18691                            clientProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;18692                        }1876718768                        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 the18776                                    // top state as well, but only if there is not another18777                                    // reason for it to be running.  Being on the top is a18778                                    // special state, meaning you are specifically running18779                                    // for the current top app.  If the process is already18780                                    // running in the background for some other reason, it18781                                    // is more important to continue considering it to be18782                                    // in the background state.18783                                    mayBeTop = true;18784                                    clientProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;18785                                } else {18786                                    // Special handling for above-top states (persistent18787                                    // processes).  These should not bring the current process18788                                    // into the top state, since they are not on top.  Instead18789                                    // give them the best state after that.18790                                    clientProcState =18791                                            ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;18792                                }18793                            }18794                        }1884118842        for (int provi = app.pubProviders.size()-1;18843                provi >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ18844                        || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE18845                        || 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 here18863                    // 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 the18886                        // top state as well, but only if there is not another18887                        // reason for it to be running.  Being on the top is a18888                        // special state, meaning you are specifically running18889                        // for the current top app.  If the process is already18890                        // running in the background for some other reason, it18891                        // is more important to continue considering it to be18892                        // in the background state.18893                        mayBeTop = true;18894                        clientProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;18895                    }


原创粉丝点击