Processes and application lifecycle

In most cases, every application runs in its own Linux process.This process is created for the application when some of its code needs tobe run, and will remain running until it is no longer needed andthe system needs lớn reclaim its memory for use by other applications.

An unusual and fundamental feature of is that an application process"slifetime is not directly controlled by the application itself.Instead, it is determined by the system through a combination of the parts of the applicationthat the system knows are running, how important these things are to the user,& how much overall memory is available in the system.

It is important thatapplication edquebecor.coms underst& how different application components(in particular Activity, Service,và BroadcastReceiver) impact the lifetimeof the application"s process. Not using these components correctly canresult in the system killing the application"s process while it is doingimportant work.

A comtháng example of a process life-cycle bug is aBroadcastReceiver that starts a thread when itreceives an Intent in its BroadcastReceiver.onReceive()method, & then returns from the function. Once it returns, the systemconsiders the BroadcastReceiver to be no longer active sầu, và thus, its hostingprocess no longer needed (unless other application components are active init). So, the system may kill the process at any time khổng lồ reclalặng memory, và in doing so,it terminates the spawned thread running in the process. The solution to lớn this problemis typically to lớn schedule a JobService from the BroadcastReceiver, so thesystem knows that there is still active work being done in the process.

To determine which processes should be killed when low on memory, edquebecor.complaces each process into lớn an "importance hierarchy" based on the components running inthem and the state of those components. These process types are (in order of importance):

A foreground process is one that is required forwhat the user is currently doing. Various application components cancause its containing process lớn be considered foreground in differentways. A process is considered to lớn be in the foreground if any of thefollowing conditions hold:

There will only ever be a few such processes in the system, và these will onlybe killed as a last resort if memory is so low that not even these processescan continue khổng lồ run. Generally, at this point, the device hasreached a memory paging state, so this action is required in order lớn keep the userinterface responsive sầu.

A visible process is doing work that the user is currently aware of,so killing it would have a noticeable negative impact on the user experience. A process isconsidered visible in the following conditions: It is hosting a service that the system is using for a particular feature that the user is aware, such as a live sầu wallpaper, đầu vào method service, etc.

The number of these processes running in the system is less bounded than foregroundprocesses, but still relatively controlled. These processes areconsidered extremely important and will not be killed unless doing so isrequired lớn keep all foreground processes running.

When deciding how to lớn classify a process, the system will base its decision on the mostimportant level found aao ước all the components currently active sầu in the process.See the Activity, Service, andBroadcastReceiver documentation for more detail on howeach of these components contribute to lớn the overall life-cycle of a process.The documentation for each of these classes describes in more detail howthey impact the overall life-cycle of their application.

A process"s priority may also be increased based on other dependenciesa process has to it. For example, if process A has bound to lớn aService withthe Context.BIND_AUTO_CREATEflag or is using aContentProvider in process B, then process B"sclassification will always be at least as important as process A"s.

< "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" , "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" , "type": "thumb-down", "id": "outOfDate", "label":"Out of date" , "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples/Code issue" , "type": "thumb-down", "id": "otherDown", "label":"Other" > < "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy lớn understand" , "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" , "type": "thumb-up", "id": "otherUp", "label":"Other" >

Content and code samples on this page are subject khổng lồ the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.




Language English Bahasa Indonesia Español – América Latina Português – Brasil 中文 – 简体 日本語 한국어