gpt4 book ai didi

android - 如何决定何时在单独的进程中运行不同的 android 应用程序组件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:53:10 24 4
gpt4 key购买 nike

我已阅读以下声明 here

By default, all components of the same application run in the same process and most applications should not change this. However, if one needs to control which process a certain component belongs to, he can do so in the manifest file. The manifest entry for each type of component element—<activity>, <service>, <receiver>, and <provider>—supports an android:process attribute that can specify a process in which that component should run. One can set this attribute so that each component runs in its own process or so that some components share a process while others do not.

我想知道开发者在哪些场景下愿意在不同的进程中运行不同的组件,这样做会有什么好处?

我读到的另一个说法是

The <application> element in the manifest file also supports an android:process attribute, to set a default value that applies to all components

关于上面的说法,我想知道开发者为什么要这样做,默认情况下已经有一个进程与一个应用程序关联,并且所有组件都在该进程中运行。

任何人都可以为我澄清这些事情,因为我在其他任何地方都没有得到任何细节

谢谢

最佳答案

让我们以 Google Chrome browser 为例充分利用了android:process属性。在此之前,让我们了解一下为什么要考虑多进程架构。

还记得那些我们使用协作式多任务操作系统的时代吗?有一个进程和应用程序用于轮流在该进程中运行。该架构的问题是,如果一个应用程序行为不当,那么单个进程就会因整个系统宕机而终止。

现在是现代操作系统,在自己的进程中运行应用程序。如果一个应用程序行为不当,托管它的进程就会终止,并且不会影响系统的其余部分。

同样适用于浏览器。如果一个网页出现问题,它会使在其他选项卡中打开的网页不可用,从而导致整个浏览器崩溃。因此构建了多进程架构。

单独的进程用于浏览器选项卡,以保护浏览器应用程序免受呈现引擎中的错误影响。每个渲染进程都在单独的进程中作为 android 服务运行。这是通过使用 android:process 完成的<service> 的标签元素。用于渲染引擎进程的另一个重要标志是 android:isolateProcess .此标志确保渲染进程无法访问网络、显示和文件系统等系统资源,从而使浏览器应用程序高度安全。

这是 chrome list 文件的 fragment :

 <service android:name="org.chromium.content.app.SandboxedProcessService0" android:permission="com.google.android.apps.chrome.permission.CHILD_SERVICE" android:exported="false" android:process=":sandboxed_process0" android:isolatedProcess="true" />

这是 adb shell 的输出:

USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME
u0_a14 12926 317 694380 102828 ffffffff 00000000 S com.android.chrome
u0_i16 26875 317 590860 59012 ffffffff 00000000 S com.android.chrome:sandboxed_process5
u0_i17 27004 317 577460 47644 ffffffff 00000000 S com.android.chrome:sandboxed_process6

The element in the manifest file also supports an android:process attribute, to set a default value that applies to all components

默认情况下,应用程序进程的名称将是 <manifest> 中指定的包名称标签。这可以通过在 android:process 中指定名称来覆盖<application> 的属性标签。一个用例:如果多个应用程序想要在同一个进程中运行,前提是这些应用程序由相同的证书签名并共享用户 ID。

如果名字<android:process>: 开头, 它变成了那个应用程序的私有(private),就像 chrome 的渲染引擎 ( com.android.chrome:sandboxed_process5 ) 一样。它意味着除com.android.chrome以外的应用程序无法与此呈现引擎通信。

如果名字<android:process>以小写字符开头,它成为全局进程。来自 docs :

This allows components in different applications to share a process, reducing resource usage.

优势总结:

  • 提高应用程序的整体稳定性(崩溃/挂起)。一个服务进程崩溃不会导致整个应用程序崩溃。
  • 通过阻止访问系统的其余部分来确保安全。
  • 通过在一个进程中运行组件并在不同的应用程序之间共享它来减少资源使用。

基本上,您应该能够分离关注点并决定应用多进程架构是否有意义。

更新 1:添加@Budius 评论

每个进程只有一定数量的可用内存。在我工作的应用程序中,我们在大型内存阵列中进行计算密集型处理。我们总是在单独的进程中触发这些计算,以确保我们有足够的内存来完成整个事情,并且不会因 OutOfMemory 而崩溃。

关于android - 如何决定何时在单独的进程中运行不同的 android 应用程序组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24114689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com