Android静态变量作用域和生命周期

发布于 2024-10-22 05:45:02 字数 233 浏览 4 评论 0原文

我有一个应用程序,它有一个服务,它使用 ArrayList在后台长时间存储数字;该变量在服务启动时初始化。

该服务位于后台,并且会频繁访问变量(这就是为什么我不想使用文件管理或设置——为了电池寿命,文件 I/O 会非常昂贵) 。

该变量在其生命周期内可能会达到 ~1MB->2MB。

可以肯定地说该变量永远不会被 GC 或系统清空吗?或者有什么方法可以阻止它吗?

I have an application that has a Service that uses an ArrayList<Double> to store numbers in the background for a very long time; the variable is initialized when the service started.

The service is in the background, and there will be frequent access to the variable (that's why I don't want to use file management or settings -- it will be very expensive for a file I/O for the sake of battery life).

The variable will likely be ~1MB->2MB over its lifetime.

Is it safe to say that the variable will never be nulled by GC or the system, or is there any way to prevent it?

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

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

发布评论

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

评论(2

夜访吸血鬼 2024-10-29 05:45:02

我有一个应用程序,它的服务使用 ArrayList 在后台长时间存储数字

如果“很长一段时间”大于“几秒钟,或者只要用户明确要求它正在运行”,那么我们就会遇到问题。

简而言之,您的服务不会“持续很长时间”。用户将使用任务杀手将其杀死,或者用户将使用设置应用程序将其杀死,或者Android将由于年龄过大而将其杀死。太多开发人员泄漏服务,导致设备性能下降。

除了短暂的时间(例如,下载大文件时)或应用户请求(例如,音乐播放器)之外,真正需要运行的服务很少。

可以肯定地说该变量永远不会被 GC 或系统清空吗?或者有什么方法可以阻止它吗?

只要进程存在,它就会存在。该进程将一直存在,直到您停止服务(假设没有其他组件在运行),或者直到用户重新启动手机,或者直到发生前面概述的任何情况(例如,任务杀手)。

I have an application that has a Service that uses an ArrayList to store numbers in the background for a very long time

If "a very long time" is greater than "a handful of seconds, or as long as the user explicitly asks for it to be running", then we have problems.

Simply put, your service will not live "for a very long time". The user will kill it off with a task killer, or the user will kill it off with Settings application, or Android will kill it off due to excessive age. Too many developers are leaking services, causing degradations in device performance.

Very few services truly need to be running except for brief periods of time (e.g., while downloading a large file) or at user request (e.g., music player).

Is it safe to say that the variable will never be nulled by GC or the system, or is there any way to prevent it?

It will live as long as the process lives. The process will live until you stop the service (assuming there are no other components running), or until the user reboots their phone, or until any of the scenarios outlined earlier (e.g., task killer) occurs.

木槿暧夏七纪年 2024-10-29 05:45:02

可以肯定地说该变量永远不会被 GC 或系统清空

是的,只要可以在代码中访问该变量即可,但这可能是好事,也可能是坏事。

静态变量的“生命周期”是类的生命周期——在加载该类时“始终且永远”——并且范围是可以访问所述稳定标识符的任何代码,例如通过可见性修饰符允许的代码,尽管也可以考虑反思。

但是,变量不会被 GC 回收 - 对象会被回收,因此显式/手动将变量设置为“null”或“虚拟对象”(当变量不被使用时) -- 如果没有其他对该变量所引用的对象的引用 -- 使所述对象符合回收条件。

但是,如果可能的话,最好避免静态值,因为这也有助于隐式对象生命周期控制。

快乐编码。

Is it safe to say that the variable will never be nulled by GC or the system

Yes, as long as the variable can be accessed in your code, but this may be a good thing or a bad thing.

The "lifetime" of a static variable is that of the class -- "always and forever" while that class is loaded -- and the scope is any code that can access said stable identifier, e.g. that which is allowed via the visibility modifiers, although reflection could also be considered.

However, variables are not GC'ed -- objects are, so explicitly/manually setting the variable to "null" or a "dummy object" when it's not being used may -- if there are no other references to the object that the variable used to refer to -- make said object eligible for reclamation.

However, it is best to avoid static values, if at all possible as this can also help with implicit object lifetime control.

Happy coding.

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