15

I am working on an app that runs as a service and waits for a message. After I check the log, I find out that Android kills and restarts many processes very often! This not only happens to my app, but is the same for many other services.

I cannot see any reason for this and my device has enough memory. I test with a Sony Xperia S running Android 4.0.4. Is this normal or a bug?

Here is a part of the log to show you what I mean:

02-04 15:02:38.791 320 332 I ActivityManager: Process com.android.email (pid 32763) has died. 02-04 15:02:38.791 320 332 W ActivityManager: Scheduling restart of crashed service com.android.email/.service.MailService in 5000ms

.... and 13 minutes later :

02-04 15:15:32.601 320 694 I ActivityManager: Process com.android.email (pid 1453) has died.

gonzobrains
  • 7,856
  • 14
  • 81
  • 132
Aki
  • 151
  • 1
  • 1
  • 3

2 Answers2

12

It's normal, the OS does this regulary.

Why? Every App or Service when inactive or in the background remains in memory until Android memory manager decides it is either taking up too much memory for nothing, or when another active app/service needs it. For example when you hit the home button when you are in your e-mail app. It will return to the exact place where you were when re-opening the app. Because this app was simply paused and in some sort of hibernation mode in the memory. Unless Android needs to allocate that memory for anything else, it keeps it there.

A way to make sure the OS does not kill your service, is creating a persistant service.

You can only make your service persistent if you are developing system apps. These services will be basically un-killable, and are labeled as "PERS" in the output of the "adb shell dumpsys activity" command.

http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/

But:

Please use AlarmManager and an IntentService, so your service does not need to be in memory except when it is doing meaningful work. This also means Android is rather unlikely to kill your service while you are in memory, and users are unlikely to kill your service because they think you are wasting memory.

Timmetje
  • 7,641
  • 18
  • 36
  • Hi,thanks for your fast answere! I know about this option, but I do not develop system apps! The point, why I wonder about the log is, that I can see on my own logfiles, that my app was killes yesterday betwenn 9:00am and 9PM only 3 times, but in this night, where my phone lay on the table and recharge it was killed 15 times and mostly with a delay of some minutes. Why so often in nhight, where nothing is to do? – Aki Feb 07 '13 at 10:12
  • It's something the O/S decides when to do it and why. Again, still normal. I do not know the details of this. If you are not making a system app, you cannot make a persistant service. And you should use AlarmManager and IntentService. This way Android won't kill your service. Because it's actually being used whern in memory. – Timmetje Feb 07 '13 at 15:08
  • Tim. If it is a chat app you need to keep the service running. My service is getting killed frequently and with very large time - 3m-9m seconds – Harshit Bangar Oct 27 '15 at 03:16
0

One more possible reason can be due to customization of Thread.UncaughtExceptionHandler for the main thread.

By default, Android OS will show an error dialog if the process is killed because of some uncaught Exception. But you can set custom Thread.UncaughtExceptionHandler and don't propagate Exception to the default Thread.UncaughtExceptionHandler, instead you can just kill the process. Then you won't see any error messages in logs except

02-04 15:02:38.791 320 332 I ActivityManager: Process com.your.app (pid 32763) has died.

and your process will die silently.

That's convenient way if you want just store Exception and don't show error dialog that might be the case for some background processes.

Yuriy
  • 1,466
  • 2
  • 14
  • 30