Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:35:53+00:00 2026-05-28T01:35:53+00:00

I have a background service that sometimes gets killed by the OS when it

  • 0

I have a background service that sometimes gets killed by the OS when it is running low on memory.

  1. How to simulate this behaviour so I can debug it?

The dev guide simply says “if your service is started, then you must design it to gracefully handle restarts by the system. If the system kills your service, it restarts it as soon as resources become available again”.

  1. What’s the sequence of calls from when it gets killed to when it finishes restarting?

On a side (related) question, what happens to an actively running AsyncTask started in the service when the service gets killed by the OS, i.e., without service.onDestroy getting called? Does it keep running or get ripped silently along with the service?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:35:54+00:00Added an answer on May 28, 2026 at 1:35 am

    Under newer versions, a service will have the following events triggered:

    onCreate()
    

    Followed by…

    int onStartCommand(Intent intent, int flags, int startid)
    

    I know in the comments above you mention using that, but it’s worth repeating: Don’t use the old “onStart()” event. onStartCommand is the new way of doing things.

    the onCreate() can be used to create any objects, etc. but do the actually code of your service in the onStartCommand().

    When done with the onStartCommand() you should return a result. Using “START_STICKY” tells the OS it can restart if it needs to kill it. Using “START_NOT_STICKY” tells the os not to bother trying to restart it after memory becomes available again. That means your application would need to manually start the service again. There are other options as well – check the API docs.

    Checking those flags will allow you to see why your service has started – if your own app launched it or if the OS launched it to restart it. You’ll need to be periodically storing the state of any important variables so that if the OS has relaunched it you can retrieve those – you could probably use a SharedPreferences private storage to store those. Definitely store any in the onDestroy event, but don’t count on that being called.

    Also, it’s recommended that you store the startID field in a variable and use it with a stopSelfResult(startId) when your service is done running.

    Keep in mind that if your service is killed by the OS you may not have the chance to store any variables. You need to be able to see if your state is where you expect when restarted by the OS and if not just reset everything or gracefully die maybe.

    As far as debugging, have you considered writing another app that does nothing but suck memory in an Activity in order to force a low memory condition? The top activity should get preference to the memory and force the service to die.

    Additional threads launched in the service are still part of the same application process, so they would be killed along with the service (and the rest of the application.) You can verify this by adding regular log statements inside the threads and then killing the service.

    Something else that might be useful for you is checking to see if your service is already running from inside your application. Here’s a function to do that:

    // Determine if one of my services is currently running
    public static boolean isMyServiceRunning(Context context, String servicename) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (servicename.equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that is running as a background service and constantly listens
I have a background service that gets data from somewhere and likes to draw
I have a service that has its own thread running on background. I'd like
This is my first post on StackOverflow! I have a background service running and
I want to have a background service running which gets started when an Image
I have a background thread that handles communication with an external service. Each time
Background: I have a WCF service deployed on my local machine that in turns
We have a Service that continuously collects sensor data on the phone. This service
I have a simple main activity with 3 buttons, and a background service that
Hi. I have create a background service application following this tutorial http://androidsourcecode.blogspot.com/2010/10/basic-android-background-service.html . But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.