Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Android, what's the difference between running processes and cached background processes?

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?

like image 951
JackWM Avatar asked Jan 10 '13 13:01

JackWM


People also ask

What does cached background process mean Android?

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.

Do cached processes drain battery?

While apps running in the background would cause battery drain, cached processes are not active processes and therefore don't need to be shut down, contrary to what a task killer does.


2 Answers

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.

like image 150
CommonsWare Avatar answered Sep 25 '22 17:09

CommonsWare


Why not look into the "Setting" app's source code.

In my Nexus 4, "Setting" -> "App" -> "Running" looks like below.

enter image description hereenter image description here


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.

like image 34
김준호 Avatar answered Sep 22 '22 17:09

김준호