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<ProcessRecord> 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<ActivityRecord> activities = new ArrayList<>();
    // any tasks this process had run root activities in
    final ArrayList<TaskRecord> recentTasks = new ArrayList<>();
    // all ServiceRecord running in this process
    final ArraySet<ServiceRecord> services = new ArraySet<>();
    // services that are currently executing code (need to remain foreground).
    final ArraySet<ServiceRecord> executingServices = new ArraySet<>();
    // All ConnectionRecord this process holds
    final ArraySet<ConnectionRecord> connections = new ArraySet<>();
    // all IIntentReceivers that are registered from this process.
    final ArraySet<ReceiverList> receivers = new ArraySet<>();
    // class (String) -> ContentProviderRecord
    final ArrayMap<String, ContentProviderRecord> pubProviders = new ArrayMap<>();
    // All ContentProviderRecord process is using
    final ArrayList<ContentProviderConnection> 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<ActivityRecord> 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家族其他的成员;

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335