AMS系列③—常见的数据结构

本文介绍AMS管理Activity时涉及到的重要的数据结构:

  • ProcessRecord:安卓系统中用于描述进程的数据结构
  • ActivityRecord:描述Activity的数据结构
  • TaskRecord:描述栈的数据结构

ProcessRecord

AMS中的ProcessRecord成员变量

变量 意义 注释
mProcessNames 数据类型为ProcessMap,以包名为key来记录ProcessRecord; All of the applications we currently have running organized by name.The keys are strings of the application package name (as returned by the package manager)
mPidsSelfLocked 数据类型为SparseArray,以进程pid为key来记录ProcessRecord All of the processes we currently have running organized by pid.
mLruProcesses 以Lru排序的ArrayList List of running applications, sorted by recent usage.
mRemovedProcesses 数据类型为ArrayList,记录所有需要强制移除的进程; Processes that are being forcibly torn down.
mProcessesToGc 数据类型为ArrayList,记录系统进入idle状态需执行gc操作的进程; List of processes that should gc as soon as things are idle.
mPendingPssProcesses 数据类型为ArrayList,记录将要收集内存使用数据PSS的进程; Processes we want to collect PSS data from.
mProcessesOnHold 数据类型为ArrayList,记录刚开机过程,系统还没与偶准备就绪的情况下, 所有需要启动的进程都放入到该队列; List of records for processes that someone had tried to start before the system was ready. We don't start them at that point, but ensure they are started by the time booting is complete.
mPersistentStartingProcesses 数据类型ArrayList,正在启动的persistent进程 List of persistent applicationsthat are in the process of being started.
mHomeProcess 记录包含home Activity所在的进程;应该是Launcher进程吧,因为AMS开启的第一个activity有一个方法叫getHomeIntent,我猜的 This is the process holding what we currently consider to be the "home" activity.
mPreviousProcess 上一次访问的进程 This is the process holding the activity the user last visited that is in a different process from the one they are currently in.

因为每个人的翻译不一样,英文才能最准确的表达金毛的意思,所以我把注释贴出来给大家参考;最重要的是前三个加红的变量;

上面提到了进程的pss data,科普一下内存相关的知识:

一般来说内存占用大小有如下规律:VSS >= RSS >= PSS >= USS

  • VSS:Virtual Set Size,虚拟消耗内存,其大小还包括了可能不在RAM中的内存, VSS 很少被用于判断一个进程的真实内存使用量。

  • RSS:Resident Set Size 实际使用物理内存(包含共享库的内存),但是这个共享库的内存只会算一次,所以RSS也不是很准确

  • PSS:Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存),按比例分配共享库的内存,如果有三个进程都使用了一个共享库,共占用了30页内存。那么PSS将认为每个进程分别占用该共享库10页的大小。 PSS是非常有用的数据,因为系统中所有进程的PSS都相加的话,就刚好反映了系统中的总共占用的内存。 而当一个进程被销毁之后, 其占用的共享库那部分比例的PSS,将会再次按比例分配给余下使用该库的进程。

  • USS: Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存),USS是非常非常有用的数据,因为它反映了运行一个特定进程真实的边际成本(增量成本)。当一个进程被销毁后,USS是真实返回给系统的内存。当进程中存在一个可疑的内存泄露时,USS是最佳观察数据。

persistent applications:

在manifest中配置的persistent属性,这个属性配置好了后会开机自启,在AMS的SystemReady中会开启所有的persistent为true的进程;

ProcessRecord中的成员变量

    int pid;                    // The process of this application; 0 if none
    final String processName;   // name of the process

    IApplicationThread thread;  // the actual proc...  may be null only if
                                // 'persistent' is true (in which case we
                                // are in the process of launching the app)

    int maxAdj;                 // Maximum OOM adjustment for this process
    int curRawAdj;              // Current OOM unlimited adjustment for this process
    int setRawAdj;              // Last set OOM unlimited adjustment for this process
    int curAdj;                 // Current OOM adjustment for this process
    int setAdj;                 // Last set OOM adjustment for this process
    int verifiedAdj;            // The last adjustment that was verified as actually being set

    // all activities running in the process
    final ArrayList activities = new ArrayList<>();
    // any tasks this process had run root activities in
    final ArrayList recentTasks = new ArrayList<>();
    // all ServiceRecord running in this process
    final ArraySet services = new ArraySet<>();
    // services that are currently executing code (need to remain foreground).
    final ArraySet executingServices = new ArraySet<>();
    // All ConnectionRecord this process holds
    final ArraySet connections = new ArraySet<>();
    // all IIntentReceivers that are registered from this process.
    final ArraySet receivers = new ArraySet<>();
    // class (String) -> ContentProviderRecord
    final ArrayMap pubProviders = new ArrayMap<>();
    // All ContentProviderRecord process is using
    final ArrayList conProviders = new ArrayList<>();

    Dialog anrDialog;           // dialog being displayed due to app not resp.
    Dialog crashDialog;         // dialog being displayed due to crash.

    boolean killedByAm;         // True when proc has been killed by activity manager, not for RAM
  • pid:进程id
  • processName :进程名,默认情况下进程名和该进程运行的第一个apk的包名是相同的,当然也可以自定义进程名;
  • thread:IApplicationThread对象,上一篇文章中的app.thread,用于进程间通信,通过attach绑定的;
  • adj:adj可以理解为一个阈值,oom,进程保活相关;内存阈值吧,有时间仔细看
  • 四大组件:运行在当前进程的四大组件
  • anr弹框和crash弹框:后续我想总结一篇ANR相关的文章;
  • killedByAm:如果这个值为true,表示这个进程被ams杀了,不是因为内存不足

ActivityRecord

在AMS中,活动是以ActivityRecord的形式记录的,由ActivityStarter创建,关于ActivityStarter会在后面的文章中介绍,先看一下ActivityRecord记录了哪些数据:

    final ActivityManagerService service; // owner
    final Intent intent;    // the original intent that generated us
    final ActivityInfo info; // all about me 
    ProcessRecord app;      // if non-null, hosting application
    private TaskRecord task;        // the task this is in.
    final String launchedFromPackage; // always the package who started the activity.
    final String taskAffinity; // as per ActivityInfo.taskAffinity
    final ActivityStackSupervisor mStackSupervisor;
    int launchMode;         // the launch mode activity attribute.
    private ActivityState mState;    // current state we are in
  • service:AMS的引用
  • intent:开启这个Activity的Intent
  • info:ActivityInfo类,包含了这个Activity的信息,包括Activity中的代码,Manifest配置信息
  • app:当前Activity所在的进程
  • task:活动栈
  • launchedFromPackage:Activity所在的包名
  • taskAffinity:Activity希望归属的栈,后面会仔细介绍这个参数
  • mStackSupervisor:ActivityStackSupervisor引用,栈管理类
  • launchMode:启动模式
  • mState:Activity的状态

TaskRecord

    final int taskId;       // Unique identifier for this task.
    String affinity;        // The affinity name for this task, or null; may change identity.
    Intent intent;          // The original intent that started the task.
    /** List of all activities in the task arranged in history order */
    final ArrayList mActivities;
    /** Current stack. Setter must always be used to update the value. */
    private ActivityStack mStack;
    final ActivityManagerService mService;
  • taskId:用来描述一个Activity任务栈
  • affinity:栈的affinity,可以在manifest为Activity指定affinity属性表示活动希望归属的栈
  • intent:开启这个栈的intent
  • mActivities:当前栈的Activity List
  • mStack:当前栈对应的ActivityStack对象
  • mService:AMS的引用

TaskRecord存储了任务栈的所有信息,包括当前栈的活动List,栈的affinity属性,以及对应ActivityStack对象,下篇会详细介绍ActivityStack以及AMS家族其他的成员;

你可能感兴趣的:(AMS系列③—常见的数据结构)