Middle+
How does the system determine which process to terminate?
sobes.tech AI
Answer from AI
The Android system uses a heuristic approach based on process priorities to determine which process to terminate when resources, typically memory, are scarce. This mechanism is called the Low Memory Killer Daemon (LMK-D), which in turn is based on the oom_score rating.
Process priorities are determined based on their type and activity:
- Foreground process (Priority 1): Actively interacts with the user. Terminated as a last resort.
- Visible process (Priority 2): Visible on the screen but not active (e.g., paused activity). Low likelihood of termination.
- Service process (Priority 3): Started by
startService(). Can run for a long time but is less important than visible processes. - Cached process (Priority 4): Not active and not started by a service. Contains cached components for faster startup. Terminated first when memory is low.
LMK-D monitors the amount of free memory and, if it falls below a certain threshold, begins terminating processes starting from the least priority.
Factors influencing oom_score:
- Type of components running (Activity, Service, BroadcastReceiver, ContentProvider).
- Visibility to the user.
- Presence of active bindings from other processes.
- Belonging to an important system service.
Thus, the system terminates processes with the highest oom_score (lowest priority) to free memory for more important processes.