Process Importance and System Process Killing
Process Importance and System Process Killing
1. What is process importance?
Android assigns an importance level to every application process.
It uses this importance level to decide which process should remain alive and which process can be killed when memory is low.
More important process
|
Kept alive longer
|
Less important process
|
Killed earlier---
2. Process importance levels
Android process importance can be understood using these main categories:
Highest importance
|
v
Foreground process
|
v
Visible process
|
v
Service process
|
v
Cached process
|
v
Lowest importance---
3. Foreground process
A foreground process is directly required for what the user is currently doing.
Examples:
* Contains a resumed Activity
* Executes a BroadcastReceiver
* Runs an important component callback
* Performs user-visible foreground work
User interacting with Activity
|
v
Foreground processKilling possibility: Very low
Android kills foreground processes only during extreme conditions.
---
4. Visible process
A visible process contains something the user can see but may not currently interact with.
Examples:
* Activity is paused but still visible
* Activity is behind a transparent dialog
* A foreground service is running with a visible notification
Activity visible
|
Not fully interactive
|
Visible processKilling possibility: Low
---
5. Service process
A service process contains a started Service doing background work.
Examples:
* Uploading data
* Downloading files
* Processing information
* Performing synchronization
No visible Activity
|
Started Service running
|
Service processKilling possibility: Medium
A normal Service does not guarantee that the process will remain alive forever.
Use:
* Foreground Service for immediate user-visible work
* WorkManager for deferrable and reliable work
---
6. Cached process
A cached process has no component that the user currently needs.
Example:
User presses Home
|
Activity.onStop()
|
Process remains in memory
|
Cached processAndroid keeps cached processes to make app reopening faster.
Killing possibility: High
Cached processes are normally killed first when Android needs memory.
---
7. Process importance table
| Process type | Example | Killing possibility |
|---|---|---|
| Foreground | Resumed Activity | Very low |
| Visible | Paused but visible Activity | Low |
| Service | Started background Service | Medium |
| Cached | Stopped Activity | High |
If a process contains multiple components, Android gives it the importance of its most important component.Example:
Stopped Activity
+
Foreground Service
|
v
Process gets higher importance
because of Foreground Service---
8. Why does Android kill a process?
Android may kill an app process because:
* Device memory is low
* App is in cached state
* App crashes
* App causes an ANR
* App consumes excessive resources
* User force-stops the app
* Application is updated
* Operating system restarts
* Package or permission state changes
---
9. What is lost after process death?
Everything stored only in memory is lost.
Application object -> Lost
Activity objects -> Lost
Fragment objects -> Lost
ViewModels -> Lost
Singleton objects -> Lost
Static variables -> Lost
Running threads -> Lost
Coroutine scopes -> Lost
In-memory cache -> LostPersistent data normally remains available.
Room database -> Remains
DataStore -> Remains
SharedPreferences -> Remains
Files -> Remain
Server data -> Remains---
10. Is onDestroy() guaranteed?
No.
When Android kills an application process, it may remove the process directly.
Android needs memory
|
v
Process killed
|
v
onDestroy() may not runNever depend on `onDestroy()` for:* Saving important data
* Completing a network request
* Updating the server
* Closing an important transaction
* Saving login information
* Finishing background work
Save important data when it changes, not when the process is about to die.
---
11. Background state versus process death
| Background state | Process death |
|---|---|
| Process may still be alive | Process no longer exists |
| Singleton data may remain | Singleton data is lost |
| ViewModel may remain | ViewModel is lost |
| Returning can be fast | New process is required |
| Application object remains | Application object is recreated |
---
Interview answer
Android assigns importance levels to processes based on their active components. Foreground processes have the highest importance, followed by visible, service and cached processes. When memory is low, Android normally kills cached processes first. Process death removes all in-memory data, including Application objects, ViewModels, singletons and static variables. Lifecycle methods such as onDestroy() are not guaranteed to run during system process killing.