简体   繁体   English

在Android上,运行进程和缓存后台进程之间的区别是什么?

[英]On Android, what's the difference between running processes and cached background processes?

On Android, when I look into "Setting" -> "App", under the tab "running", I can see the memory is cut into to parts: "used memory" and "memory free", also the applications are either put into "used memory", or "memory free". 在Android上,当我查看“设置” - >“应用程序”时,在“运行”选项卡下,我可以看到内存被切入到部分:“已用内存”和“无内存”,应用程序也被放入进入“二手记忆”,或“无记忆”。 The applications in "memory free" part are noted as "cached background process". “无内存”部分中的应用程序被标记为“缓存后台进程”。

So, what are "cached background processes"? 那么,什么是“缓存后台进程”? They are still in memory, rather than switched to "disk" (as desktops/laptops do), right? 它们仍然在内存中,而不是切换到“磁盘”(就像台式机/笔记本电脑那样),对吧? When the user tab one of these "cached background processes", it would be displayed immediately as it is still in memory, just like a running process, right? 当用户选中其中一个“缓存的后台进程”时,它会立即显示,因为它仍在内存中,就像正在运行的进程一样,对吧?

What does Android do when it "cache" an application? Android在“缓存”应用程序时会做什么?

So, what are "cached background processes"? 那么,什么是“缓存后台进程”?

Since you are asking for a technical interpretation of something listed in a device UI, the definition may vary by device, if device manufacturers elected to tinker with the Settings app. 由于您要求对设备UI中列出的内容进行技术解释,因此如果设备制造商选择修改“设置”应用,则定义可能因设备而异。

That being said, "cached background processes" usually refers to processes that do not have a foreground activity and do not have a running service. 话虽这么说,“缓存的后台进程”通常是指没有前台活动且没有正在运行的服务的进程。 These processes are kept in memory simply because we have enough memory to do so, and therefore, as you note, the user can switch back to these processes quickly. 这些进程只是因为我们有足够的内存来保存在内存中,因此,正如您所注意到的,用户可以快速切换回这些进程。 As Android starts to need more system RAM for yet other processes, the "cached background processes" tend to be the processes that get terminated to free up system RAM. 由于Android开始需要更多的系统RAM用于其他进程,“缓存的后台进程”往往是被终止以释放系统RAM的进程。

The pre-eminent example of a "cached background process" would be one where the user launched the app, poked around it briefly, then pressed HOME to return to the home screen. “缓存后台进程”的一个突出示例是用户启动应用程序,短暂戳它,然后按HOME返回主屏幕。 If the process does not have a running service, I would expect to find it listed as a "cached background process". 如果进程没有正在运行的服务,我希望将其列为“缓存的后台进程”。

They are still in memory, rather than switched to "disk" (as desktops/laptops do), right? 它们仍然在内存中,而不是切换到“磁盘”(就像台式机/笔记本电脑那样),对吧?

Correct. 正确。 Android devices do not use swap space. Android设备不使用交换空间。

Why not look into the "Setting" app's source code . 为什么不查看“设置”应用程序的源代码

In my Nexus 4, "Setting" -> "App" -> "Running" looks like below. 在我的Nexus 4中,“设置” - >“应用程序” - >“正在运行”如下所示。

在此输入图像描述在此输入图像描述


Before getting started, there are five levels in the importance hierarchy in Android Process. 在开始之前,Android Process中的重要性层次结构中有五个级别。 These are 这些是

1) Foreground process, 1)前景过程,
2) Visible process, 2)可见过程,
3) Service process, 3)服务流程,
4) Background process, 4)背景过程,
5) Empty process 5)空过程

You can find more details at "Processes and Threads" document in Android Developer site . 您可以在Android Developer站点的“进程和线程”文档中找到更多详细信息。

I did look into code, and it turned out "SHOW CACHED PROCESSES" shows those processes whose importance hierarchy is equal to or lower than "Background process" . 我确实研究过代码,事实证明“SHOW CACHED PROCESSES”显示了那些重要性层次等于或低于“后台进程”的进程 On the other hand, "SHOW RUNNING SERVICES" shows those whose importance hierarchy is equal to "Visible process" or higher . 另一方面, “显示运行服务”显示其重要性等级等于“可见过程”或更高的那些 I dropped some detail to clearly show main point. 我放弃了一些细节以清楚地表明要点。 You can see the full source code of this part here . 您可以在此处查看此部分的完整源代码。

try {
        final int numProc = mAllProcessItems.size();
        int[] pids = new int[numProc];
        for (int i=0; i<numProc; i++) {
            pids[i] = mAllProcessItems.get(i).mPid;
        }

        ...

        for (int i=0; i<pids.length; i++) {
            ProcessItem proc = mAllProcessItems.get(i);
            changed |= proc.updateSize(context, pss[i], mSequence);
            if (proc.mCurSeq == mSequence) {
                serviceProcessMemory += proc.mSize;
            } else if (proc.mRunningProcessInfo.importance >=
                    ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                backgroundProcessMemory += proc.mSize;
                MergedItem mergedItem;
                if (newBackgroundItems != null) {
                    mergedItem = proc.mMergedItem = new MergedItem(proc.mUserId);
                    proc.mMergedItem.mProcess = proc;
                    diffUsers |= mergedItem.mUserId != mMyUserId;
                    newBackgroundItems.add(mergedItem);
                } else {
                   ...
                }

               ...

            } else if (proc.mRunningProcessInfo.importance <=
                    ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                foregroundProcessMemory += proc.mSize;
            }
        }
    } catch (RemoteException e) {
    }


So, back to your question, 那么,回到你的问题,

They are still in memory, rather than switched to "disk" (as desktops/laptops do), right? 它们仍然在内存中,而不是切换到“磁盘”(就像台式机/笔记本电脑那样),对吧?

Yes, they are still in memory , but eventually Android system may need to remove old processes to reclaim memory for new or more important processes. 是的,它们仍在内存中 ,但最终Android系统可能需要删除旧进程以回收新内存或更重要进程的内存。 To determine which processes to keep and which to kill, the system places each process into an "importance hierarchy". 为了确定要保留哪些进程以及要杀死哪些进程,系统将每个进程放入“重要性层次结构”。

When the user tab one of these "cached background processes", it would be displayed immediately as it is still in memory, just like a running process, right? 当用户选中其中一个“缓存的后台进程”时,它会立即显示,因为它仍在内存中,就像正在运行的进程一样,对吧?

Right . For example, the only reason to keep "Empty process" alive is to improve startup time the next time a component needs to run in it. 例如, 保持“空进程”存活的唯一原因是下次组件需要在其中运行时改善启动时间。

What does Android do when it "cache" an application? Android在“缓存”应用程序时会做什么?

AFAIK, it simply do not kill the process and keep the resources to immediately respond to User when he/she comes back. AFAIK, 它只是不会杀死进程并保持资源在他/她回来时立即回复用户。

Process ranks 流程排名

The Android operating system tries to maintain the application running for as long as possible, but when the available memory is low, it will try to free resources in the system by killing the processes with lower importance frst. Android操作系统试图尽可能长时间地维持应用程序的运行,但是当可用内存不足时,它将尝试通过终止具有较低重要性的进程来释放系统中的资源。

This is when process ranking comes into the picture; 这是流程排名进入画面的时候; the Android processes are ranked in the next fve categories from the higher priority to the lower priorities: Android流程在从较高优先级到较低优先级的下一个fve类别中排名:

  • Foreground process : This is a process that hosts an activity or service that the user is currently interacting with: a service started in the foreground or service running its life cycle callbacks 前台进程 :这是一个托管用户当前正在与之交互的活动或服务的进程:在前台启动的服务或运行其生命周期回调的服务
  • Visible process : This is a process that hosts a paused activity or service bounded to a visible activity 可见进程 :这是一个承载暂停活动或服务的进程,该活动或服务受可见活动限制
  • Service process: This is a process that hosts a service not bound to a visible activity 服务进程:这是一个承载未绑定到可见活动的服务的进程
  • Background process : This is a process that hosts a non-visible activity; 后台进程 :这是一个承载不可见活动的进程; all background processes are sorted over a Least-Recently-Used (LRU) list, therefore, the most recently used processes are the last killed processes when they 所有后台进程都按最近最少使用(LRU)列表排序,因此,最近使用的进程是最后一次被杀死的进程
  • Empty process : This is a process used to cache inactive Android components and to improve any component startup time 空进程 :这是一个用于缓存非活动Android组件并改善任何组件启动时间的过程

When the system reaches a point that it needs to release resources, the processes available to be killed will be sorted, taking into account the process rank, last used processes, and components running. 当系统到达需要释放资源的点时,将对可用于杀死的进程进行排序,同时考虑进程排名,上次使用的进程和运行的组件。

Source : 来源

Asynchronous Android Programming - Second Edition - Helder Vasconcelos - July 2016 异步Android编程 - 第二版 - Helder Vasconcelos - 2016年7月

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM