Search code examples
androidbroadcastreceiversleepalarmmanager

Static variables remain vaild after main process dies?


I have an android recurring service running in background, no Activity running in front of it. Each and every 15min the Service is invoked by an AlamManager (RTC_WAKEUP) using WakeLocks to perform some network action for a cupple of seconds and then it dies. The service uses a static variable storing a PendingIntent object each time it get called. Btw: I know about SharedPreferences or using persistent layout, thats not my question.

My real question is: How could it be that my static variables are still valid? Even I the meantime of 15 min inactivity (real device sleep, does it matter anyway?) my static variables seems valid after all. I thought and read, if no Activity or Service running in the main UI-Thread and/or another worker threads the whole process dies, and the static vaules are erased, too.

If and why android keeps my process still hanging arround, keeping all static variables. An does it correlate with my scheduled AlamManager event. I daresay android keeps my process memory valid!?


Solution

  • How could it be that my static variables are still valid?

    Android did not terminate your process between the alarms.

    I thought and read, if no Activity or Service running in the main UI-Thread and/or another worker threads the whole process dies, and the static vaules are erased, too.

    Android can terminate your process at any time. Having no running components (activities or services) increases the odds that Android will terminate your process before it terminates other processes. However, if Android does not need the RAM, it will leave your process alone.

    However, since you don't know whether your process will be around each time your AlarmManager event occurs, you need to handle the case where your static data members need to be initialized.