-1

The title isn't good, so please read this description to understand what i mean.

I created a background service that i want to run for a long time, by definition services on android can run even if user switch the app or even closes it.

My question is: how does android management system knows which threads to keep and which to wipe when the app goes background and the only running thing is the service?

My service runs by default on the mainthread, so when i want to perform a long task i do: AsyncTask.THREAD_POOL_EXECUTOR.execute(runnable);

is that right? can i use this default threadpool created by android or the service must explicitly creates the thread in order to the system knows that thread must survive even if the app goes background?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rafael Lima
  • 3,079
  • 3
  • 41
  • 105

1 Answers1

2

how does android management system knows which threads to keep and which to wipe when the app goes background and the only running thing is the service?

Android does not "wipe" any threads. Android terminates processes to free up system RAM, not threads.

is that right?

I would recommend using your own Executor, RxJava, or Kotlin coroutines, over using the executor in AsyncTask. That is because I expect AsyncTask to be deprecated sometime, as it is "old school".

However, beyond that, what you are doing sounds fine.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • your answer makes a lot ofsense but it opens me a new question: if android deals with "processes" and the service and the application are the same process (as they share memory and stuff i can only guess they are the same process) so if my app goes background for a very long time, but i do keep a foreground service doing a long task it means most of the memory my app alocates will still be hold right? I mean, all the activities can be desroyed all other things are still hold right? – Rafael Lima Jul 13 '19 at 20:31
  • @RafaelLima: "it means most of the memory my app alocates will still be hold right?" -- yes, though you can override methods like `onTrimMemory()` to help with that (e.g., flush caches). Some apps specifically use two processes: one for the long-lived service, and one for the short-term UI. That simplifies this memory management problem but makes lots of other things more complicated, so I am not a big fan of that approach, though for some projects it will be useful. – CommonsWare Jul 13 '19 at 20:37