Android from the foundation · Puzzle 12 of 16

Starting an app, cold, warm and hot

Opening an app can mean very different amounts of work, depending on whether its process and its screen still exist.

The problem

In puzzles 10 and 11, a new app process was forked from Zygote. But is a process forked every time you open an app? Sometimes the app opens instantly, sometimes it takes a second. What's the difference?

See it

Three rows: cold start forks a process, creates the Application and the Activity; warm start only creates the Activity; hot start creates nothing and brings the screen to the front kernel coldwarmhot slowestfasterfastest process ApplicationActivity process Application Activity process ApplicationActivity to the front
  1. Opening an app can need three things: a process, one instance of your app's Application class (one per process, for setup the whole app needs), and the Activity on screen.

    Step 1 of 4
  2. Cold start: no process yet (never started, or Android ended it to free memory). Fork from Zygote, create the Application (its Application.onCreate() runs), then the Activity. The slowest.

    Step 2 of 4
  3. Warm start: the process is alive, but the Activity was destroyed. Only the Activity is created again. Predict: what's left to do if both are alive?

    Step 3 of 4
  4. Nothing to create. Hot start: system_server just brings the screen back to the front: started, then resumed.

    Step 4 of 4

The building block

A process isn't kept forever: when memory runs short, Android ends the processes of apps that aren't on screen (in the background), so coming back to an app can need a fresh one.

Your app has one instance of its Application class per process: a place for setup the whole app needs, like opening the file where the app keeps its saved data. It's created right after the fork, and its callback Application.onCreate() runs before any Activity exists.

  • Cold start: no process yet. Fork from Zygote, create the Application, then create the first Activity.
  • Warm start: the process is still alive, but the Activity was destroyed (puzzle 3), for example because the user closed it, so it's created again.
  • Hot start: process and Activity are both alive, stopped in the background; system_server just brings the screen back to the front.

The analogy

Cold: build the house from the show house, move in, set up the room. Warm: the house stands, set up the room again. Hot: everything's ready, just open the curtains.

Check yourself

You put slow setup (opening a big saved-data file) in Application.onCreate(). Which starts get slower?

Right. Application.onCreate() runs once per process, in a cold start, before any screen.

Not quite. Hot starts create nothing new. The Application only appears in a cold start.

Not quite. The Application is created once per process. Warm and hot starts reuse the living process.

The user comes back to your app a minute later and it opens instantly. Which kind of start was that likely?

Right. Hot starts reuse everything.

Not quite. A cold start does the most work: fork, Application, Activity.

What's the difference between a warm and a cold start?

Not quite. That's a hot start. A warm start creates the Activity again.

Right. Warm: process alive, screen rebuilt. Cold: everything from scratch.

Sources

What you now know

Cold start = fork Zygote, create your Application, then the first Activity. Warm = process alive, Activity created again. Hot = both alive, just brought to the front.