Specifically, are cached processes no longer scheduled on the CPU?
I don't usually think of processes being scheduled on a CPU. I think of threads as being scheduled on a CPU. Perhaps we are just using the terms differently.
A cached process' threads are no different than any other process' threads. Ideally, a cached process only has threads that are blocked waiting on something (e.g., IPC from a core OS process, telling the app process to start another activity in response to the user pressing a icon from the home screen). However, there is nothing stopping an app from having leaked some thread that continues running, for however long that process remains cached.
For example, you could create an app with a single activity like this:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Executors
.newSingleThreadScheduledExecutor()
.scheduleAtFixedRate({ Log.e("BadWolves", "Zombie!") }, 5, 5, TimeUnit.SECONDS)
finish()
}
}
Here, I fork a zombie thread, then finish()
the activity. The process moves into the cached state fairly quickly, once the activity is destroyed. Yet, the zombie thread continues logging to LogCat.
How long it logs to LogCat varies by OS version and perhaps manufacturer tweaks. So, for example, on the Pixel 2 that I just tossed this onto, it has been logging for 10 minutes, which frankly is longer than I would have expected on Android 8.1.
This would mean that it was the duty of every app in existence to implement pausing correctly when backgrounded.
Yes, to an extent. The OS can terminate your process at any point, and cached processes are prime candidates to be terminated when system RAM is needed. So, leaked threads usually don't live all that long, because cached processes usually don't live all that long. Part of the reason why my zombie is staggering around as long as it is is that this device isn't used for a lot, so I don't have a lot of processes coming and going, minimizing pressure on system RAM.
Now, if you'll excuse me, I need to kill a zombie...