Android 中的服务开销(我应该创建多少个)

发布于 2024-12-15 02:31:32 字数 523 浏览 1 评论 0原文

有谁知道 Android 中的服务开销有多大。
如果还有任何其他论点帮助我做出以下设计决策。

我有两个 SQLite 数据库,一个存储实际数据(基本上只读产品库存),另一个存储设置、所选项目列表等。

现在我创建了一项服务来负责管理这些数据库,主要有两个原因

  • :能够“关闭时保存”(设置),这可以通过 onDestroy
  • 加载数据需要一些时间,因此快速关闭应用程序会将数据保留在内存中

从设计角度来看,我可以

  • :一项服务处理两个数据库
  • 创建两个服务,每个服务处理一个数据库

为每个数据库创建一个单独的服务感觉更干净,例如扩展通用基类来处理关闭、计时器等。它还允许我独立配置它们(这不是必需的)现在)。

另一方面,我不想开始走这条路,然后,当我习惯在“服务”中做这样的事情时,发现服务的数量限制为 3 或 5 个。

那么,与托管 5 个不同功能的一个服务相比,例如 5 个正在运行的服务的开销如何?有什么想法吗?

Does anybody know how big the overhead for services in Android is.
And if there are any other argument helping me with the following design decision.

I have two SQLite Dbs, one storing actual data (basically read only product inventory), the other storing settings, lists of selected items, etc.

Now I created a service to take care of manageing these databases, for two main reasons:

  • I want to be able to "save on close" (settings), which is possible with the onDestroy
  • It takes some time to load the data, so a quick close of the app keeps the data in memory

From a design perspective, I could either:

  • Create one service handling both DBs
  • Create two services, each handling one DB

It feels cleaner to create a seperate service for each DB, e.g. extending a generic base class to take care of shutdown, timers, etc. It also allows me to configure them independent (which is NOT required right now).

On the other hand, I do not want to start going down this road, and then, when I am used to doing stuff like this in "services" discovering that there is a limit of 3 or 5 services.

So how is the Overhead of e.g. 5 running services, vs. one service hosting 5 different features? Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热风软妹 2024-12-22 02:31:32

在您的情况下,Android 有效支持的服务数量应该无需担心。在看到一些任务管理器中正在运行的服务列表后,我倾向于说在大多数设备上最多约 100 个“正在运行”的服务应该没有问题。

但是,我不确定您想要做的事情实际上是否应该在服务中实现。您能详细说明为什么您认为您需要服务吗?

我希望能够“关闭时保存”(设置),这可以通过 onDestroy 实现

您不能在正常的 Activity 生命周期回调中执行此操作吗?

加载数据需要一些时间,因此快速关闭应用程序会将数据保留在内存中

“快速关闭”是什么意思?您是否正在尝试为数据库数据构建某种缓存

The number of services effectively supported by Android should be nothing to worry about in your situation. Having seen lists of running services in some task managers, I tend to say that up to ~100 "running" services should be no problem on most devices.

However, I'm not sure if what you're trying to do actually should be implemented in a service. Can you elaborate why you think you need a service?

I want to be able to "save on close" (settings), which is possible with the onDestroy

Can't you do this in your normal Activity lifecycle callbacks?

It takes some time to load the data, so a quick close of the app keeps the data in memory

What do you mean by "quick close"? Are you trying to build some sort of cache for the DB data?

合约呢 2024-12-22 02:31:32

来自 Android 开发文档...

谨慎使用服务

http://developer.android.com/training/articles/memory。 html#服务

From the Android dev docs...

Use services sparingly

http://developer.android.com/training/articles/memory.html#Services

长发绾君心 2024-12-22 02:31:32

如果服务是 RemoteServices,那么它们会在单独的进程中启动,这会增加开销并且占用大量内存。

尝试对这两种情况使用单一服务。

If services are RemoteServices then they are launched in a separate process which add overhead and is memory intensive.

Try using a single service for both the scenarios.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文