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?
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).
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.