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 process

Killing 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 process

Killing 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 process

Killing 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 process

Android 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 typeExampleKilling possibility
ForegroundResumed ActivityVery low
VisiblePaused but visible ActivityLow
ServiceStarted background ServiceMedium
CachedStopped ActivityHigh
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       -> Lost

Persistent 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 run
Never 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 stateProcess death
Process may still be aliveProcess no longer exists
Singleton data may remainSingleton data is lost
ViewModel may remainViewModel is lost
Returning can be fastNew process is required
Application object remainsApplication 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.