Android是否会杀死每个服务或整个进程? [英] Does Android kill each Service or the whole Process?

查看:169
本文介绍了Android是否会杀死每个服务或整个进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们中的许多人都知道,(宽泛地说)Android中的Service如果不调用Service.startForeground()将会被系统杀死.但这是整个故事吗...?

Many of us know that (in loose terms) a Service in Android will get killed by the system if it does not call Service.startForeground(). But is this the whole story...?

我正在处理一些旧代码,将系统"权限应用程序移植到Play商店.该应用程序具有5个后台服务(不调用startForeground()),由于运行该应用程序具有系统"权限,因此这些服务到目前为止是安全的.它已安装在自定义设备上.由于时间紧迫且预算有限,将这5重构为1并不是短期的解决方案,但我们希望尽快开放beta.

I am working on some legacy code, porting a "system" permission app to the Play store. This app has 5 background services (that do not call startForeground()), which until now, were safe, due to the "system" permission that the app ran with. It was installed on a custom device. Due to tight timelines and budgets, refactoring these 5 into 1 is not a short term solution, but we would like to move to open beta as soon as possible.

简短的问题是:

  • 如果没有前台ActivityService,Android会杀死每个单独的后台服务,还是只是杀死进程本身?
  • If there is no foreground Activity or Service, does Android kill each individual background service, or does it just kill the process itself?

以下是进程和线程上的Android文档中的一些信息,其中讨论了如何Android将在压力下终止进程:

Here is some info from the Android docs on Processes and Threads that discusses how Android will terminate processes when under pressure:

Android可能会决定在内存中的某个时刻关闭进程 较低,并且需要其他更立即的过程 为用户服务.在以下过程中运行的应用程序组件 因此,被杀害者被销毁.的流程再次开始 这些组件需要再次工作时.

Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. Application components running in the process that's killed are consequently destroyed. A process is started again for those components when there's again work for them to do.

在确定要杀死哪些进程时,Android系统会权衡其 对用户的相对重要性.例如,它更容易关闭 记录不再可见的活动的流程 屏幕,而不是托管可见活动的过程. 决定 因此,是否终止进程取决于进程的状态. 组件在该过程中运行. ...

When deciding which processes to kill, the Android system weighs their relative importance to the user. For example, it more readily shuts down a process hosting activities that are no longer visible on screen, compared to a process hosting visible activities. The decision whether to terminate a process, therefore, depends on the state of the components running in that process. ...


常识说:" Android将终止服务"

个人经验表明, Android也会杀死该进程本身(如上面引文所述).这可以通过将对象存储在Application对象中,并指出在服务被终止后它们会重新初始化来显示.

Personal experience has shown that Android will also kill the process itself (as described in the quote above). This can be shown by storing objects in the Application object, and noting that they get reinitialised after the service is killed.

考虑到上述情况,有一些解决问题的选项:

将5个服务重构为1个,在各种不同的线程上运行.将1服务置于前台.问题解决了.

Refactor the 5 services into 1, running on various different threads. Bring the 1 service into the foreground. Problem solved.

不幸的是,目前没有预算,由于项目的时间表,我们希望找到快速解决方案.

Unfortunately no budget for this at the moment, and we would prefer to find a quick fix due to project timelines.

这是最终实现的最终解决方案,将投入全面生产.

This is the final solution that will be implemented going forwards into full production.

在前台启动每个服务,每个服务都有其自己的Notification图标.

Start each service in foreground, each with its own Notification icon.

这是一个麻烦的解决方案,但可以在Beta现场试验中使用,请给我们一些时间.

This is a messy solution, but will work for the beta field trials, buying us some time.

我认为这是强力"方法.

I think of this as the "brute force" approach.

如果杀死的是进程,而不是每个单独的服务,那么足以运行单个前台服务就足够了.

If it is the process that is killed, rather than each individual service, then it will be good enough to have a single foreground service running.

这将防止" Android杀死进程(即降低其概率).

This would "prevent" (i.e. lower the probability of) Android from killing the process.

所有这5种服务将因此存活.

All 5 services would thus survive.

​​关于服务的文档告诉我们,如果服务绑定到另一个上下文,则

The docs on Services tell us that if a service is bound to another context, then

stopService()或stopSelf()实际不会停止服务,直到 所有客户解除绑定.

stopService() or stopSelf() does not actually stop the service until all clients unbind.

如果我从单个前台服务绑定到其他服务,是否会使它们全部保持活动状态?

If I bind to the other services from a single foreground service, will that keep them all alive?

  • Android是否会杀死每个未绑定的后台服务?
  • 还是只是杀死应用程序所在的虚拟机?
  • Does Android kill each unbound, background Service?
  • Or does it just kill the VM that the application is running in?

18 41小时的测试#3(受一个服务保护的进程)之后,所有6个服务仍在运行(5个旧服务加上1个新服务).

After 18 41 hours of testing #3 (Process protected by one Service), all 6 services are still running (5 old plus the 1 new).

因此,如果没有前台活动或服务正在运行,Android似乎将杀死进程.

So it is looking as if Android would kill the process if no foreground activities or services are running.

推荐答案

如果没有前台活动或服务,Android是否会杀死每个活动或服务 单独的后台服务,还是只是终止了进程 本身?

If there is no foreground Activity or Service, does Android kill each individual background service, or does it just kill the process itself?

Android不会杀死单个ActivitiesServices,这没有多大意义.例如,如果Activity位于后台,则Android永远不会决定专门杀死该Activity.内存中使用哪种Java Object都没有关系,所有这些Object实例都具有相同的命运:如果不再需要/使用它们,则将对其进行垃圾回收.人们经常谈论Activity在后台时被杀死,这确实具有误导性,因为他们只是说它可以被垃圾收集并最终被清除.因此,这种垃圾收集将破坏对象的特定实例.与内存不足的设备无关.

Android does not kill individual Activities or Services, that wouldn't make much sense. For example if an Activity is in the background Android will never decide to specifically kill this one Activity. It doesn't matter what kind of Java Object is in memory all those Object instances share the same fate: If they are no longer needed/used they will be garbage collected. People often talk about an Activity being killed when it is in the background and that is really misleading because they just mean it can be garbage collected and eventually will be. So this garbage collecting is what would destroy a specific instance of an object. It has nothing to do with the device having low memory.

当Android内存不足时,它将决定终止整个进程,正如您已经在文档中阅读的一样,它会选择最不重要的进程.

When Android is running out of memory it will decide to kill a whole process, and as you already read in the documentation it chooses the least important ones.

我想告诉你的是,这是两个根本不同的过程.一个是Android OS在内存耗尽时杀死不重要的进程,另一个是垃圾收集器,垃圾收集器一直在寻找不再可用的,可以释放的内存.

What I'm trying to tell you is that those are two fundamentally different processes. One is the Android OS killing not important processes when its running out of memory and the other one is the garbage collector which is constantly looking for no longer used memory which it can free.

常识说,"Android将终止服务"

Common knowledge says that "Android will kill the Service"

正如我在上面解释的那样,这可能会引起误解,并且并非完全正确,如果设备内存不足,它将杀死整个过程,而不仅仅是特定的Service.这与Service不再使用时被垃圾回收的情况不同.

As I explained above, this can be misleading and is not completely true, if the device is running out of memory it will kill the whole process, not just a specific Service. This is different from the Service being garbage collected when it is no longer used.

现在介绍您的解决方案:

And now on to your solutions:

将5个服务重构为1个,在各种不同的线程上运行. 将1服务置于前台.问题解决了.

Refactor the 5 services into 1, running on various different threads. Bring the 1 service into the foreground. Problem solved.

这显然是最好的选择,但是正如您所说的,您现在无法实现.

This obviously is the best option but as you said you are not able to implement this now.

在前台启动每个服务,每个服务都有其自己的通知"图标.

Start each service in foreground, each with its own Notification icon.

这将是一种不好的解决方案,没有理由有多个通知.其他选择显然更好.

This would be a kind of bad solution, there is no reason to have multiple notifications. The other options are clearly better.

如果杀死的是进程,而不是每个人 服务,那么只有一个前台就足够了 服务正在运行.

If it is the process that is killed, rather than each individual service, then it will be good enough to have a single foreground service running.

肯定可以,我会尝试的.

This could definitely work, I would try it.

关于服务的文档告诉我们,如果一项服务绑定到另一个 上下文,然后

The docs on Services tell us that if a service is bound to another context, then

stopService()或stopSelf()实际不会停止服务,直到 所有客户解除绑定.

stopService() or stopSelf() does not actually stop the service until all clients unbind.

如果我从单个前台服务绑定到其他服务,将 让他们都活着?

If I bind to the other services from a single foreground service, will that keep them all alive?

这是我认为的次佳选择.

This is the next best option in my opinion.

我最初考虑过以自己的进程启动每个Service.但是我不确定这是否适用于您.特别是如果您想始终保持所有 Services运行.在自己的进程中启动Service的好处显然是,它独立于应用程序的其余部分.因此,即使某些部分由于内存限制而被杀死,该应用程序的其余部分也将继续在其他进程中运行.

I initially thought about starting each Service in its own process. But I am not sure if this is applicable to you. Especially if you want to keep all Services running at all times. The benefit of starting a Service in its own process is obviously that it is independent from the rest of the app. So even if some part is killed due to memory constraints the rest of the app will keep running in the other process.

您是否曾经考虑过使用继承来解决问题?这可能是实现选项 1)的最简单方法.

Have you ever thought about using inheritance to solve the problem? It might be the simplest way to implement your option 1).

这篇关于Android是否会杀死每个服务或整个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆