Android will kill your service if it runs out of memory, but you can do a couple of things to recover from it.
The first thing you can try is to use the front-end services, the front-end service is high-priority services that will not be killed if it is not completely necessary (note that these services increase battery consumption). You can find it here using compatibility with older devices, otherwise you only need to call startForeground inside your service.
Another thing you can do is use some flags in your service to restart it when it is killed by the OS. You can use 2 different flags (depending on what behavior you want to reproduce).
START_STICKY will restart your service with no intention, so each time you need to restore the data necessary to start your service.
START_REDELIVER_INTENT, in this case, your service will be restarted with the latest information about intentions (you may perform your service several times with different information when you want a different behavior).
Flags can be used as follows:
@Override public int onStartCommand(Intent intent, int flags, int startId) {
Depending on what you need, use one or the other.
Hope this helps :)
source share