Search code examples
androidjava-native-interface

Does android kill the JVM when stopping services?


I have an application that uses a Native pthread to run a long running task and I have a background service without notification keeping a reference to the thread through JNI.

When the service is destroyed I lose the reference to the pthread.

I would like to store this in a static / Singleton so it survives the service being stopped. My question is : when does the JVM die? Does android recreate a JVM when it restarts the service?

Thanks


Solution

  • Android can kill your process at any time, if it is not running a foreground app (note that this condition can be stolen from your app at any moment), and all the threads will be gone. When the framework restarts your service, it will be a new process, with other threads.

    See https://stackoverflow.com/a/24956128/192373 for detailed explanations.