On Android, when I look into "Setting" -> "App", under the tab "running", I can see the memory is cut into to parts: "used memory" and "memory free", also the applications are either put into "used memory", or "memory free". The applications in "memory free" part are noted as "cached background process".
So, what are "cached background processes"? They are still in memory, rather than switched to "disk" (as desktops/laptops do), right? When the user tab one of these "cached background processes", it would be displayed immediately as it is still in memory, just like a running process, right?
What does Android do when it "cache" an application?
So, what are "cached background processes"?
Since you are asking for a technical interpretation of something listed in a device UI, the definition may vary by device, if device manufacturers elected to tinker with the Settings app.
That being said, "cached background processes" usually refers to processes that do not have a foreground activity and do not have a running service. These processes are kept in memory simply because we have enough memory to do so, and therefore, as you note, the user can switch back to these processes quickly. As Android starts to need more system RAM for yet other processes, the "cached background processes" tend to be the processes that get terminated to free up system RAM.
The pre-eminent example of a "cached background process" would be one where the user launched the app, poked around it briefly, then pressed HOME to return to the home screen. If the process does not have a running service, I would expect to find it listed as a "cached background process".
They are still in memory, rather than switched to "disk" (as desktops/laptops do), right?
Correct. Android devices do not use swap space.
Why not look into the "Setting" app's source code.
In my Nexus 4, "Setting" -> "App" -> "Running" looks like below.
Before getting started, there are five levels in the importance hierarchy in Android Process. These are
1) Foreground process,
2) Visible process,
3) Service process,
4) Background process,
5) Empty process
You can find more details at "Processes and Threads" document in Android Developer site.
I did look into code, and it turned out "SHOW CACHED PROCESSES" shows those processes whose importance hierarchy is equal to or lower than "Background process". On the other hand, "SHOW RUNNING SERVICES" shows those whose importance hierarchy is equal to "Visible process" or higher. I dropped some detail to clearly show main point. You can see the full source code of this part here.
try {
final int numProc = mAllProcessItems.size();
int[] pids = new int[numProc];
for (int i=0; i<numProc; i++) {
pids[i] = mAllProcessItems.get(i).mPid;
}
...
for (int i=0; i<pids.length; i++) {
ProcessItem proc = mAllProcessItems.get(i);
changed |= proc.updateSize(context, pss[i], mSequence);
if (proc.mCurSeq == mSequence) {
serviceProcessMemory += proc.mSize;
} else if (proc.mRunningProcessInfo.importance >=
ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
backgroundProcessMemory += proc.mSize;
MergedItem mergedItem;
if (newBackgroundItems != null) {
mergedItem = proc.mMergedItem = new MergedItem(proc.mUserId);
proc.mMergedItem.mProcess = proc;
diffUsers |= mergedItem.mUserId != mMyUserId;
newBackgroundItems.add(mergedItem);
} else {
...
}
...
} else if (proc.mRunningProcessInfo.importance <=
ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
foregroundProcessMemory += proc.mSize;
}
}
} catch (RemoteException e) {
}
So, back to your question,
They are still in memory, rather than switched to "disk" (as desktops/laptops do), right?
Yes, they are still in memory, but eventually Android system may need to remove old processes to reclaim memory for new or more important processes. To determine which processes to keep and which to kill, the system places each process into an "importance hierarchy".
When the user tab one of these "cached background processes", it would be displayed immediately as it is still in memory, just like a running process, right?
Right. For example, the only reason to keep "Empty process" alive is to improve startup time the next time a component needs to run in it.
What does Android do when it "cache" an application?
AFAIK, it simply do not kill the process and keep the resources to immediately respond to User when he/she comes back.
Process ranks
The Android operating system tries to maintain the application running for as long
as possible, but when the available memory is low, it will try to free resources in the
system by killing the processes with lower importance frst.
This is when process ranking comes into the picture; the Android processes are
ranked in the next fve categories from the higher priority to the lower priorities:
- Foreground process: This is a process that hosts an activity or service that
the user is currently interacting with: a service started in the foreground or
service running its life cycle callbacks
- Visible process: This is a process that hosts a paused activity or service
bounded to a visible activity
- Service process: This is a process that hosts a service not bound to a
visible activity
- Background process: This is a process that hosts a non-visible activity; all
background processes are sorted over a Least-Recently-Used (LRU) list,
therefore, the most recently used processes are the last killed processes when
they
- Empty process: This is a process used to cache inactive Android components
and to improve any component startup time
When the system reaches a point that it needs to release resources, the processes
available to be killed will be sorted, taking into account the process rank, last used
processes, and components running.
Source :
Asynchronous Android Programming - Second Edition - Helder Vasconcelos - July 2016