2

When I use an app such as "Go Power Master" to forcibly kill Services running on my Android phone, not all Services restart with the same delay. Why is this and how do I reduce the time taken for my Service to restart?

The Facebook Service is a perfect example. Below is its LogCat output while it was killed 3 times in succession. Notice the restart times in bold: 14992ms, 5000ms, 14963ms.

My Service is not treated so well. Below is its LogCat output while it was killed 3 times in succession. Notice the much larger restart times in bold: 23358ms, 93432ms, 373728ms.

The complete source code for this project is on GitHub. https://github.com/ccoffey/NUIMWiFi

Facebook LogCat

05-10 14:09:33.381: I/ActivityManager(192): Killing proc 7280:com.facebook.katana/10077: kill background 05-10 14:09:33.381: W/ActivityManager(192): Scheduling restart of crashed service com.facebook.katana/.service.MediaUploadService in 14992ms 05-10 14:09:48.412: I/ActivityManager(192): Start proc com.facebook.katana for service com.facebook.katana/.service.MediaUploadService: pid=7847 uid=10077 gids={3003, 1006, 1015} 05-10 14:09:48.568: I/ActivityThread(7847): Pub com.facebook.katana.provider.LoggingProvider: com.facebook.katana.provider.LoggingProvider 05-10 14:09:48.592: I/ActivityThread(7847): Pub com.facebook.katana.provider.KeyValueProvider: com.facebook.katana.provider.KeyValueProvider 05-10 14:09:48.592: I/ActivityThread(7847): Pub com.facebook.katana.provider.CacheProvider: com.facebook.katana.provider.CacheProvider 05-10 14:09:48.592: I/ActivityThread(7847): Pub com.facebook.katana.provider.MailboxProvider: com.facebook.katana.provider.MailboxProvider 05-10 14:09:48.599: I/ActivityThread(7847): Pub com.facebook.katana.provider.UserStatusesProvider: com.facebook.katana.provider.UserStatusesProvider 05-10 14:09:48.599: I/ActivityThread(7847): Pub com.facebook.katana.provider.EventsProvider: com.facebook.katana.provider.EventsProvider 05-10 14:09:48.607: I/ActivityThread(7847): Pub com.facebook.katana.provider.NotificationsProvider: com.facebook.katana.provider.NotificationsProvider 05-10 14:09:48.607: I/ActivityThread(7847): Pub com.facebook.katana.provider.UserValuesProvider: com.facebook.katana.provider.UserValuesProvider 05-10 14:09:48.607: I/ActivityThread(7847): Pub com.facebook.katana.provider.PagesProvider: com.facebook.katana.provider.PagesProvider 05-10 14:09:48.607: I/ActivityThread(7847): Pub com.facebook.katana.provider.MobileEventLogProvider: com.facebook.katana.provider.MobileEventLogProvider 05-10 14:09:48.607: I/ActivityThread(7847): Pub com.facebook.katana.provider.PushNotificationsProvider: com.facebook.katana.provider.PushNotificationsProvider 05-10 14:09:48.615: I/ActivityThread(7847): Pub com.facebook.katana.provider.PhotosProvider: com.facebook.katana.provider.PhotosProvider 05-10 14:09:48.615: I/ActivityThread(7847): Pub com.facebook.katana.provider.ConnectionsProvider: com.facebook.katana.provider.ConnectionsProvider 05-10 14:09:48.623: I/ActivityThread(7847): Pub com.facebook.orca.notify.FbandroidMessagesForegroundProvider: com.facebook.orca.notify.FbandroidMessagesForegroundProvider 05-10 14:09:48.639: D/ACRA(7847): ACRA is enabled for com.facebook.katana, intializing... 05-10 14:09:48.654: D/ACRA(7847): Looking for error files in /data/data/com.facebook.katana/app_acra-reports 05-10 14:09:48.701: W/nalizableReferenceQueue(7847): at com.facebook.orca.inject.binder.AnnotatedBindingBuilderImpl.a(AnnotatedBindingBuilderImpl.java:22) 05-10 14:09:48.701: W/nalizableReferenceQueue(7847): at com.facebook.orca.app.FbBaseModule.a(FbBaseModule.java:73) 05-10 14:09:48.701: W/nalizableReferenceQueue(7847): at com.facebook.orca.inject.AbstractModule.a(AbstractModule.java:19) 05-10 14:09:48.701: W/nalizableReferenceQueue(7847): at com.facebook.orca.inject.FbInjectorImpl.a(FbInjectorImpl.java:61) 05-10 14:09:48.701: W/nalizableReferenceQueue(7847): at com.facebook.orca.inject.FbInjectorImpl.(FbInjectorImpl.java:41) 05-10 14:09:48.701: W/nalizableReferenceQueue(7847): at com.facebook.orca.inject.FbInjector.a(FbInjector.java:40) 05-10 14:09:48.701: W/nalizableReferenceQueue(7847): at com.facebook.katana.FacebookApplication.onCreate(FacebookApplication.java:75) 05-10 14:09:48.928: I/SqliteDatabaseCpp(7847): sqlite returned: error code = 21, msg = misuse at line 105099 of [8609a15dfa], db=/data/data/com.facebook.katana/databases/prefs_db 05-10 14:09:53.810: I/ActivityManager(192): Killing proc 7847:com.facebook.katana/10077: kill background 05-10 14:09:53.810: W/ActivityManager(192): Scheduling restart of crashed service com.facebook.katana/.service.MediaUploadService in 5000ms 05-10 14:09:58.842: I/ActivityManager(192): Start proc com.facebook.katana for service com.facebook.katana/.service.MediaUploadService: pid=7890 uid=10077 gids={3003, 1006, 1015} 05-10 14:09:59.053: I/ActivityThread(7890): Pub com.facebook.katana.provider.LoggingProvider: com.facebook.katana.provider.LoggingProvider 05-10 14:09:59.060: I/ActivityThread(7890): Pub com.facebook.katana.provider.KeyValueProvider: com.facebook.katana.provider.KeyValueProvider 05-10 14:09:59.060: I/ActivityThread(7890): Pub com.facebook.katana.provider.CacheProvider: com.facebook.katana.provider.CacheProvider 05-10 14:09:59.076: I/ActivityThread(7890): Pub com.facebook.katana.provider.MailboxProvider: com.facebook.katana.provider.MailboxProvider 05-10 14:09:59.076: I/ActivityThread(7890): Pub com.facebook.katana.provider.UserStatusesProvider: com.facebook.katana.provider.UserStatusesProvider 05-10 14:09:59.076: I/ActivityThread(7890): Pub com.facebook.katana.provider.EventsProvider: com.facebook.katana.provider.EventsProvider 05-10 14:09:59.076: I/ActivityThread(7890): Pub com.facebook.katana.provider.NotificationsProvider: com.facebook.katana.provider.NotificationsProvider 05-10 14:09:59.076: I/ActivityThread(7890): Pub com.facebook.katana.provider.UserValuesProvider: com.facebook.katana.provider.UserValuesProvider 05-10 14:09:59.076: I/ActivityThread(7890): Pub com.facebook.katana.provider.PagesProvider: com.facebook.katana.provider.PagesProvider 05-10 14:09:59.084: I/ActivityThread(7890): Pub com.facebook.katana.provider.MobileEventLogProvider: com.facebook.katana.provider.MobileEventLogProvider 05-10 14:09:59.084: I/ActivityThread(7890): Pub com.facebook.katana.provider.PushNotificationsProvider: com.facebook.katana.provider.PushNotificationsProvider 05-10 14:09:59.084: I/ActivityThread(7890): Pub com.facebook.katana.provider.PhotosProvider: com.facebook.katana.provider.PhotosProvider 05-10 14:09:59.084: I/ActivityThread(7890): Pub com.facebook.katana.provider.ConnectionsProvider: com.facebook.katana.provider.ConnectionsProvider 05-10 14:09:59.092: I/ActivityThread(7890): Pub com.facebook.orca.notify.FbandroidMessagesForegroundProvider: com.facebook.orca.notify.FbandroidMessagesForegroundProvider 05-10 14:09:59.154: D/ACRA(7890): ACRA is enabled for com.facebook.katana, intializing... 05-10 14:09:59.185: D/ACRA(7890): Looking for error files in /data/data/com.facebook.katana/app_acra-reports 05-10 14:09:59.232: W/nalizableReferenceQueue(7890): at com.facebook.orca.inject.binder.AnnotatedBindingBuilderImpl.a(AnnotatedBindingBuilderImpl.java:22) 05-10 14:09:59.232: W/nalizableReferenceQueue(7890): at com.facebook.orca.app.FbBaseModule.a(FbBaseModule.java:73) 05-10 14:09:59.232: W/nalizableReferenceQueue(7890): at com.facebook.orca.inject.AbstractModule.a(AbstractModule.java:19) 05-10 14:09:59.232: W/nalizableReferenceQueue(7890): at com.facebook.orca.inject.FbInjectorImpl.a(FbInjectorImpl.java:61) 05-10 14:09:59.232: W/nalizableReferenceQueue(7890): at com.facebook.orca.inject.FbInjectorImpl.(FbInjectorImpl.java:41) 05-10 14:09:59.232: W/nalizableReferenceQueue(7890): at com.facebook.orca.inject.FbInjector.a(FbInjector.java:40) 05-10 14:09:59.232: W/nalizableReferenceQueue(7890): at com.facebook.katana.FacebookApplication.onCreate(FacebookApplication.java:75) 05-10 14:10:44.826: I/ActivityManager(192): Killing proc 7890:com.facebook.katana/10077: kill background 05-10 14:10:44.826: W/ActivityManager(192): Scheduling restart of crashed service com.facebook.katana/.service.MediaUploadService in 14963ms

MyService LogCat

I/ActivityManager( 192): Killing proc 8556:ie.cathalcoffey.android/10033: kill background I/ActivityManager( 192): Killing proc 8606:ie.cathalcoffey.android:remote/10033: kill background W/ActivityManager( 192): Scheduling restart of crashed service ie.cathalcoffey.android/.MyService in 23358ms I/ActivityManager( 192): Start proc ie.cathalcoffey.android:remote for service ie.cathalcoffey.android/.MyService: pid=8726 uid=10033 gids={3003} I/ActivityManager( 192): Killing proc 8726:ie.cathalcoffey.android:remote/10033: kill background W/ActivityManager( 192): Scheduling restart of crashed service ie.cathalcoffey.android/.MyService in 93432ms I/ActivityManager( 192): Start proc ie.cathalcoffey.android:remote for service ie.cathalcoffey.android/.MyService: pid=9063 uid=10033 gids={3003} I/ActivityManager( 192): Killing proc 9063:ie.cathalcoffey.android:remote/10033: kill background W/ActivityManager( 192): Scheduling restart of crashed service ie.cathalcoffey.android/.MyService in 373728ms

Cathal Coffey
  • 1,105
  • 2
  • 20
  • 35

3 Answers3

3

The solution to this problem seems to be the following. Cause the OS to believe that it has killed a Service that was doing work. It then appears to restart that Service with a smaller delay.

I have now edited my Service to start an AsyncTask which simply loops forever and calls Thread.sleep(100)

Now when I forcibly kill my Service, I see very similar restart times that I do for the Facebook Service: 5000ms, 24713ms, 14997ms, 54912ms, 14988ms.

Please prove me wrong or right but as it stands this seems to solve my problem.

Cathal Coffey
  • 1,105
  • 2
  • 20
  • 35
  • Wow. Never knew something like this but what about sleep mode and don't you think it will hog battery. Isn't it easier to use alarm manager. – Harshit Bangar Oct 27 '15 at 09:22
1

I believe it greatly depends on the importance of the process that was killed. When you use a task killer to kill an application, the Android OS kills it the same way it would if the system was running low on memory. Since it is killed in the same way, applications or services will be restarted by the Android OS based on the "importance" of the application.

To make a service more important (thus being restarted quicker) you could add a notification to the service. This notification would be visible in the notification bar. Since the service was visible to the user, it will be restarted quicker than applications or other services that were not visible.

Also, a service that is bounded to an Activity will have greater importance than an unbounded service.

For more information check out this http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html (The process life cycle portion)

EDIT Get importance of all running processes.

List<RunningAppProcessInfo> procInfo = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfo.size(); i++){
    int importance = procInfo.get(i).importance;
    //Print importance and Package name to log
}
  • As far as I can tell the Facebook MediaUpload Service is not bounded to an Activity (it can exist without any Activity) and provides no notification (it might display a notification under certain circumstances... but it did not during the timing experiments above) – Cathal Coffey May 10 '12 at 13:47
  • Interesting, if the service was doing something when it was killed, then it should also have higher importance. Also, just to play around you may try logging the importance of different processes then play around with your service to try to make it more important. Sorry I wasn't more help. –  May 10 '12 at 14:10
  • Actually you have been a great help so far. I've been struggling with this for a few days. I think your above code will really help me boost the importance of my Service. – Cathal Coffey May 10 '12 at 14:19
  • Good, I would be interested to hear what you discover. And maybe someone who is more knowledgable than me will come along and provide more help! –  May 10 '12 at 14:42
0
        W/ActivityManager: Scheduling restart of crashed service com.faceopen.edgedevice/.service.FaceOpenRemoteService in 1000ms
        W/ActivityManager: Scheduling restart of crashed service com.faceopen.edgedevice/.service.FaceOpenRemoteService in 4000ms
        W/ActivityManager: Scheduling restart of crashed service com.faceopen.edgedevice/.service.FaceOpenRemoteService in 16000ms
        W/ActivityManager: Scheduling restart of crashed service com.faceopen.edgedevice/.service.FaceOpenRemoteService in 64000ms
        W/ActivityManager: Scheduling restart of crashed service com.faceopen.edgedevice/.service.FaceOpenRemoteService in 256000ms

I was facing the similar issue in my Galaxy Tab on restarting the killed process. Above is the log depicting the time period assigned by the Android OS kernel to restart the process. The scheduling period in above case grew exponentially when the process was repeatedly killed as soon as it was re-starting.

However on analyzing the pattern of process scheduling by the OS, we get to the conclusion that after a fixed amount of time, the OS stops deprioritizing the scheduling of killed processes.

In our case the fixed time was 60000ms(60 seconds). Thus in other words, if we restart the kill process after 60 seconds, it was starting after 1000ms. Hence OS will not schedule the process exponentially after that.

garry
  • 419
  • 5
  • 10