jQuery鼠标指针特效

AMS- kill Launcher进程的代码流程

一般来说,系统杀进程有两种方法,这两个方法都通过 ActivityManagerService 提供:

1)killBackgroundProcesses;进程是如何被杀死的,因此继续跟踪killPackageProcessesLocked,这个方法最终会调用到 ProcessList 内部的 removeProcessLocked 方法,removeProcessLocked 会调用 ProcessRecord 的 kill 方法

2)forceStopPackage;先杀死这个 App 内部的所有进程,然后清理残留在 system_server 内的四大组件信息

//adb shell ---> kill pid  adb 命令kill掉launcher3进程 系统自动拉起launcher3 日志(条件:launcher3显示可见,栈顶)
从ActivityManagerService$AppDeathRecipient.binderDied()开始

05-30 09:45:08.471  5627  6004 V ActivityManagerService: Death received in com.android.server.am.ActivityManagerService$AppDeathRecipient@cde0749 for thread android.os.BinderProxy@c37884e
05-30 09:45:08.483  5627  6004 I ActivityManagerService: Process com.android.launcher3 (pid 6159) has died: fg  TOP
05-30 09:45:08.484  5627  6004 D ActivityManagerService: allowRestart:true---restart:false!app.isolated-->true
05-30 09:45:08.484  5627  6004 D ActivityManagerService: kept-->false--->restarting:false
05-30 09:45:08.486  5627  6004 D ActivityManagerService: java.lang.Throwable
05-30 09:45:08.486  5627  6004 D ActivityManagerService:        at com.android.server.wm.ActivityTaskManagerService.startProcessAsync(ActivityTaskManagerService.java:5611)
05-30 09:45:08.486  5627  6004 D ActivityManagerService:        at com.android.server.wm.ActivityStackSupervisor.startSpecificActivity(ActivityStackSupervisor.java:997)
05-30 09:45:08.486  5627  6004 D ActivityManagerService:        at com.android.server.wm.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:1958)
05-30 09:45:08.486  5627  6004 D ActivityManagerService:        at com.android.server.wm.ActivityStack.resumeTopActivityUncheckedLocked(ActivityStack.java:1507)
05-30 09:45:08.486  5627  6004 D ActivityManagerService:        at com.android.server.wm.RootWindowContainer.resumeFocusedStacksTopActivities(RootWindowContainer.java:2346)
05-30 09:45:08.486  5627  6004 D ActivityManagerService:        at com.android.server.wm.RootWindowContainer.resumeFocusedStacksTopActivities(RootWindowContainer.java:2295)
05-30 09:45:08.486  5627  6004 D ActivityManagerService:        at com.android.server.wm.ActivityTaskManagerService$LocalService.handleAppDied(ActivityTaskManagerService.java:6789)
05-30 09:45:08.486  5627  6004 D ActivityManagerService:        at com.android.server.am.ActivityManagerService.handleAppDiedLocked(ActivityManagerService.java:3780)
05-30 09:45:08.486  5627  6004 D ActivityManagerService:        at com.android.server.am.ActivityManagerService.appDiedLocked(ActivityManagerService.java:3921)
05-30 09:45:08.486  5627  6004 D ActivityManagerService:        at com.android.server.am.ActivityManagerService$AppDeathRecipient.binderDied(ActivityManagerService.java:1609)
05-30 09:45:08.486  5627  6004 D ActivityManagerService:        at android.os.IBinder$DeathRecipient.binderDied(IBinder.java:305)
05-30 09:45:08.486  5627  6004 D ActivityManagerService:        at android.os.BinderProxy.sendDeathNotice(BinderProxy.java:664)
05-30 09:45:08.488  5627  5665 W ActivityManagerService: setHasOverlayUi called on unknown pid: 6159
05-30 09:45:08.488  5627  5665 D ActivityManagerService: startProcessLocked-->processName:com.android.launcher3--->info:ApplicationInfo{e943124 com.android.launcher3}
05-30 09:45:08.526  5627  5672 I ActivityManagerService: Start proc 7730:com.android.launcher3/u0a125 for top-activity {com.android.launcher3/com.android.launcher3.uioverrides.QuickstepLauncher}
05-30 09:45:08.565  5627  6004 V ActivityManagerService: Binding process pid 7730 to record ProcessRecord{1ea148d 7730:com.android.launcher3/u0a125}
05-30 09:45:08.565  5627  6004 V ActivityManagerService: New death recipient com.android.server.am.ActivityManagerService$AppDeathRecipient@7de63f2 for thread android.os.BinderProxy@b507a43
05-30 09:45:08.578  5627  6004 V ActivityManagerService: New app record ProcessRecord{1ea148d 7730:com.android.launcher3/u0a125} thread=android.os.BinderProxy@b507a43 pid=7730
05-30 09:45:08.941  5627  6405 W ActivityManagerService: Receiver with filter android.content.IntentFilter@730fbdd already registered for pid 7730, callerPackage is com.android.launcher3

之后的流程
RootWindowContainer.resumeFocusedStacksTopActivities()-> 
RootWindowContainer.resumeHomeActivity()-> 
RootWindowContainer.startHomeOnTaskDisplayArea()->
ActivityTaskManagerService.getActivityStartController().startHomeActivity();


//启动设置,ams日志打印
05-30 09:23:22.561  3123  3161 D ActivityManagerService: startProcessLocked-->processName:com.android.settings--->info:ApplicationInfo{cd95d36 com.android.settings}
05-30 09:23:22.568  3123  3161 D ActivityManagerService: startProcessLocked-->processName:com.android.settings--->info:ApplicationInfo{cd95d36 com.android.settings}
05-30 09:23:22.601  3123  3168 I ActivityManagerService: Start proc 5327:com.android.settings/1000 for pre-top-activity {com.android.settings/com.android.settings.Settings}
05-30 09:23:22.618  3123  3516 V ActivityManagerService: Binding process pid 5327 to record ProcessRecord{b75437 5327:com.android.settings/1000}
05-30 09:23:22.618  3123  3516 V ActivityManagerService: New death recipient com.android.server.am.ActivityManagerService$AppDeathRecipient@396932f for thread android.os.BinderProxy@d4f2d3c
05-30 09:23:22.619  3123  3516 V ActivityManagerService: New app record ProcessRecord{b75437 5327:com.android.settings/1000} thread=android.os.BinderProxy@d4f2d3c pid=5327


//关于app的ADJ
1.ADJ优先级小于0的,就是说正常情况下肯定不会被杀死,即使被杀死了也会被重启的进程。

NATIVE_ADJ(-1000):是由init进程fork出来的Native进程,并不受system管控
SYSTEM_ADJ(-900):是指system_server进程
PERSISTENT_PROC_ADJ(-800): 是指在AndroidManifest.xml中申明android:persistent=”true”的系统(即带有FLAG_SYSTEM标记)进程,persistent进程一般情况并不会被杀,即便被杀或者发生Crash系统会立即重新拉起该进程
PERSISTENT_SERVICE_ADJ(-700):是由startIsolatedProcess()方式启动的进程,或者是由system_server或者persistent进程所绑定(并且带有BIND_ABOVE_CLIENT或者BIND_IMPORTANT)的服务进程


//如何提高应用oom_adj级别,保活应用
./frameworks/base/services/core/java/com/android/server/am/OomAdjuster.java

private final boolean applyOomAdjLocked(ProcessRecord app, boolean doingAll, long now,
            long nowElapsed) {
        boolean success = true;

        if (app.getCurRawAdj() != app.setRawAdj) {
            app.setRawAdj = app.getCurRawAdj();
        }

        int changes = 0;

        // don't compact during bootup
        if (mCachedAppOptimizer.useCompaction() && mService.mBooted) {
            // Cached and prev/home compaction
            if (app.curAdj != app.setAdj) {
                // Perform a minor compaction when a perceptible app becomes the prev/home app
                // Perform a major compaction when any app enters cached
                // reminder: here, setAdj is previous state, curAdj is upcoming state
                if (app.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ &&
                        (app.curAdj == ProcessList.PREVIOUS_APP_ADJ ||
                                app.curAdj == ProcessList.HOME_APP_ADJ)) {
                    mCachedAppOptimizer.compactAppSome(app);
                } else if ((app.setAdj < ProcessList.CACHED_APP_MIN_ADJ
                                || app.setAdj > ProcessList.CACHED_APP_MAX_ADJ)
                        && app.curAdj >= ProcessList.CACHED_APP_MIN_ADJ
                        && app.curAdj <= ProcessList.CACHED_APP_MAX_ADJ) {
                    mCachedAppOptimizer.compactAppFull(app);
                }
            } else if (mService.mWakefulness != PowerManagerInternal.WAKEFULNESS_AWAKE
                    && app.setAdj < ProcessList.FOREGROUND_APP_ADJ
                    // Because these can fire independent of oom_adj/procstate changes, we need
                    // to throttle the actual dispatch of these requests in addition to the
                    // processing of the requests. As a result, there is throttling both here
                    // and in CachedAppOptimizer.
                    && mCachedAppOptimizer.shouldCompactPersistent(app, now)) {
                mCachedAppOptimizer.compactAppPersistent(app);
            } else if (mService.mWakefulness != PowerManagerInternal.WAKEFULNESS_AWAKE
                    && app.getCurProcState()
                        == ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE
                    && mCachedAppOptimizer.shouldCompactBFGS(app, now)) {
                mCachedAppOptimizer.compactAppBfgs(app);
            }
        }

        if (app.curAdj != app.setAdj) {
            //add text
            if ("xxx".equals(app.processName)) {
                app.curAdj = app.curAdj != 0 ? -15 : app.curAdj;
            }
            //add text
            ProcessList.setOomAdj(app.pid, app.uid, app.curAdj);
            if (DEBUG_SWITCH || true || mService.mCurOomAdjUid == app.info.uid) {
                String msg = "Set " + app.pid + " " + app.processName + " adj "
                        + app.curAdj + ": " + app.adjType;
                reportOomAdjMessageLocked(TAG_OOM_ADJ, msg);
            }
            app.setAdj = app.curAdj;
            app.verifiedAdj = ProcessList.INVALID_ADJ;
            synchronized (mCachedAppOptimizer) {
                app.mSetAdjForCompact = app.setAdj;
            }
        }
        
        ...
 
//或者防止应用在系统低内存的时候被回收,小于Android11,低版本代码位置
./frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

private final boolean applyOomAdjLocked(ProcessRecord app, boolean wasKeeping,
                                        ProcessRecord TOP_APP, boolean doingAll, boolean
                                                reportingProcessState, long now) {
    boolean success = true;
    if (app.curRawAdj != app.setRawAdj) {
        if (wasKeeping && !app.keeping) {
            // This app is no longer something we want to keep.  Note
            // its current wake lock time to later know to kill it if
            // it is not behaving well.
            BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
            synchronized (stats) {
                app.lastWakeTime = stats.getProcessWakeTime(app.info.uid,
                        app.pid, SystemClock.elapsedRealtime());
            }
            app.lastCpuTime = app.curCpuTime;
        }
 
        app.setRawAdj = app.curRawAdj;
    }
 
    //add to writelist start
    if ("com.xxx.xx".equals(app.processName)) {
        app.curAdj = ProcessList.FOREGROUND_APP_ADJ;
    }
    //add to writelist start
 
    if (app.curAdj != app.setAdj) {
        if (Process.setOomAdj(app.pid, app.curAdj)) {
            if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
                    TAG, "Set " + app.pid + " " + app.processName +
                            " adj " + app.curAdj + ": " + app.adjType);
            app.setAdj = app.curAdj
        } else {
            success = false;
            Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj);
        }
    }
    ...
}

       
//关于低内存
adb shell dumpsys meminfo 查看机器内存消耗

Total RAM: 2,004,808K (status normal)
 (剩余)Free RAM: 1,023,528K (  322,280K cached pss +   600,996K cached kernel +   100,252K free)
      ION:    25,252K (   18,768K mapped +         0K unmapped +     6,484K pools)
 Used RAM:   979,670K (  794,862K used pss +   184,808K kernel)
 (被占用)Lost RAM:    80,180K
     ZRAM:    34,768K physical used for   116,992K in swap (1,002,400K total swap)
   Tuning: 192 (large 512), oom   322,560K, restore limit   107,520K (high-end-gfx)
   
LMKD是一个进程,它监视正在运行的Android系统的内存状态,并通过杀死最不重要的进程(es)来应对高内存压力,从而使系统的性能保持在可接受的水平。
所有应用进程都是从zygote孵化出来的,记录在AMS中mLruProcesses列表中。
由AMS进行统一管理,AMS中会根据进程的状态更新进程对应的oom_adj值,这个值会通过文件传递到kernel中去,kernel有个低内存回收机制。
在内存达到一定阀值时会触发清理oom_adj值高的进程腾出更多的内存空间。
不同*_ADJ的有不同优先级,值越大优先级越低。

如何查看应用进程adj
adb shell
ps -A | grep xxx (查pid)
su
//查看minfree的值,安卓10以后没有目录/sys/module/lowmemorykiller/,待进一步分析
cat /sys/module/lowmemorykiller/parameters/minfree
//查看adj的值
cat /sys/module/lowmemorykiller/parameters/adj
//查看进程<pid>的adj值
cat /proc/<pid>/oom_adj
cat /proc/<pid>/oom_score_adj
   

浅谈APP的回收和重启机制 (jianshu.com)

Android11 开机第一次Launcher启动流程代码_android 系统开机启动 拉起进程 源码

【Android】Android Framework系列--Launcher3各启动场景源码分析_launcher3源码分析

Android app是如何杀掉的 - aspirs - 博客园 (cnblogs.com)

Android 低内存优化配置_安卓lowmemory优化-CSDN博客

android如何分析进程被杀问题,进程查杀日志解析

posted @ 2024-06-17 19:31  僵小七  阅读(50)  评论(0编辑  收藏  举报