AMS进程管理--LRU篇

AMS是Android操作系统中的一个核心组件,它的全称:Activity Manager Service,但它不仅管理Activity,更是Android应用程序的管理器。负责管理应用的生命周期、任务栈(Android12后委托给ATMS管理)、进程和活动之间的切换等。

由于AMS的代码量十分庞大,需要有针对性的研究才不会陷入源码的海洋无法自拔。本文将结合源码对AMS的进程管理展开分析。本文基于Android12分析,以前的版本的源码略有差异,比如进程管理优先级的代码,12以前是放到AMS中,12之后,主要委托到了ProcessList类中,但核心思想和算法没有改变。

核心思想

对队列中的进程进行调整,根据hasActivity,hasService条件,将整个队列划分为三个区域。最后根据LRU算法,对进程所在的区域中调整位置。

分析过程已经贴在代码注释中。

ps:由于分析是本人的理解,难免有误差,所以会保存源码中的英文注释。

核心代码

final void updateLruProcessLocked(ProcessRecord app, boolean activityChange,
            ProcessRecord client) {
        final boolean hasActivity = app.hasActivitiesOrRecentTasks() || app.hasClientActivities()
                || app.treatLikeActivity;
        final boolean hasService = false; // not impl yet. app.services.size() > 0;
        if (!activityChange && hasActivity) {
            // The process has activities, so we are only allowing activity-based adjustments
            // to move it.  It should be kept in the front of the list with other
            // processes that have activities, and we don't want those to change their
            // order except due to activity operations.
            //进程中的activity没有改变,就不需要调整
            return;
        }

        //id自增,保证唯一性
        mLruSeq++;
        final long now = SystemClock.uptimeMillis();
        //保存更新调整后的时间
        app.lastActivityTime = now;

        // First a quick reject: if the app is already at the position we will
        // put it, then there is nothing to do.
        //快速校验,如果当前有包含act
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值