Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 104 ยท Getting Started

Android Versions, Boot Process, Zygote and Garbage Collection

Understand how Android evolved and how it works inside: version and API level history, the system architecture, the boot sequence, Zygote and app start, the APK build tools and garbage collection.

Most Android tutorials start with a layout and a button. A developer who also knows how the platform itself works debugs faster and answers interview questions with confidence: Why does my app need a minSdk? Why does the first start after an update take so long? Why does a leak crash the app after an hour? What happens between pressing the power button and seeing a launcher icon?

This lesson is the "inside view". It covers the version history and API levels, the layers of the system, the boot sequence, how Zygote starts your app, how an APK is built and signed, and how the garbage collector decides what to free. For the compiler and runtime details (DEX, ART, multidex) see DEX, ART and desugaring; for the four app components see Android components. Everything here is concept, so the TryIts are plain Kotlin simulations of the ideas.

Versions, API levels and code names

Every Android release has three identifiers: a marketing version (14), an API level (34) and, until Android 10, a dessert code name. Your code and Gradle files use the API level.

Version Name API level Headline changes
1.0 (2008) none 1 First release: Market, browser, Gmail, Maps, slide-out hardware keyboard
1.5 Cupcake 3 On-screen keyboard, widgets
2.2 Froyo 8 JIT compiler in Dalvik
4.0 Ice Cream Sandwich 14 One code base for phones and tablets, Holo design
5.0 Lollipop 21 ART replaces Dalvik, Material Design
6.0 Marshmallow 23 Runtime permissions, Doze battery saving
7.0 Nougat 24 Split-screen multi-window, hybrid JIT and AOT, direct-reply notifications
8.0 Oreo 26 Notification channels, background limits
9 Pie 28 Gesture navigation, display cutouts
10 none 29 Scoped storage begins, dark theme
11 none 30 One-time permissions, package visibility
12 none 31 Material You, approximate location
13 none 33 Notification permission, per-app language
14 none 34 Foreground service types, partial media access
15 none 35 Edge-to-edge by default, 16 KB page size support
16 none 36 Predictive back by default, adaptive layout rules

Two things changed between the first and the modern releases. Android 1.0 had no on-screen keyboard, no copy and paste and no app store beyond "Market"; apps had broad access and no runtime permission prompts. Android 6.0 introduced runtime permissions, so users can say no, and Android 7.0 made big shared-screen changes (multi-window) that taught developers never to assume a full screen. Each later version tightens background work, storage access and privacy. That trend is why your lessons on permissions, services and storage matter.

Which levels does my app use?

Your build.gradle.kts declares three numbers (see Gradle for Android):

  • minSdk: the oldest API level your app installs on.
  • targetSdk: the level you tested against; it opts in to that version's behavior changes. Google Play requires a recent value.
  • compileSdk: the SDK you compile against, which gives you the newest APIs at compile time.

Calling an API newer than the device's level crashes, so you guard it:

Kotlin
// read-only example
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {   // API 33
    requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1)
}
Kotlin Which features can this device use?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The system architecture, top to bottom

flowchart TD
    A["Applications<br/>(Dialer, Launcher, your app)"] --> F["Java API framework<br/>(Activity, Content Providers, View System)"]
    F --> N["Native C/C++ libraries<br/>(libc, SQLite, OpenGL ES, FreeType, WebKit-based WebView, media)"]
    F --> R["Android Runtime (ART)<br/>core libraries"]
    N --> H["Hardware abstraction layer"]
    R --> H
    H --> K["Linux kernel<br/>(drivers, memory, power, binder IPC)"]

Apps and the framework are Kotlin/Java; native libraries and ART sit beside them; the kernel is Linux with Android additions.

  • Applications: system apps (phone, contacts, launcher) and yours are the same kind of thing; any of them can be replaced by a user's choice.
  • Java API framework: the managers you call: ActivityManager, PackageManager, NotificationManager, LocationManager, the View system and Content Providers.
  • Native libraries: libc (Bionic), SQLite for databases, OpenGL ES and Vulkan for graphics, FreeType for fonts, the WebView engine (Chromium-based in modern Android; older versions used WebKit) and the media framework.
  • ART and core libraries: run your compiled code (see the runtime lesson).
  • HAL: per-device drivers behind stable interfaces.
  • Linux kernel: processes, memory, networking and security, plus Android-specific parts: Binder (the inter-process communication used between apps and system services, see AIDL and IPC), wakelocks and low-memory killer.

What happens when the phone boots

The boot chain hands control from small, trusted code to bigger and bigger components:

flowchart LR
    A["Boot ROM<br/>(in the chip)"] --> B["Bootloader<br/>verifies and loads kernel"]
    B --> C["Linux kernel<br/>starts drivers"]
    C --> D["init (PID 1)<br/>reads init.rc, starts daemons"]
    D --> E["Zygote<br/>preloads classes"]
    E --> F["system_server<br/>starts system services"]
    F --> G["Launcher (home screen)"]

Each stage starts the next and checks it (verified boot) before trusting it.

  1. Boot ROM is code burned into the chip. It finds and loads the bootloader.
  2. The bootloader initializes basic hardware, verifies the kernel's signature (verified boot) and loads it. Unlocking the bootloader turns this check off for custom ROMs.
  3. The kernel sets up memory, drivers and the file systems and starts the first user-space process, init.
  4. init (PID 1) reads init.rc scripts, mounts partitions, starts native daemons (such as adbd, the logging daemon and surfaceflinger) and launches Zygote.
  5. Zygote starts ART, loads the most-used framework classes and resources once, and then waits for requests.
  6. Zygote forks system_server, the process that hosts ActivityManager, PackageManager, WindowManager and dozens more services.
  7. When services are ready, ActivityManager starts the launcher and the system sends BOOT_COMPLETED to apps that registered for it (see Broadcast receivers).

After a system update, the first boot is slower because apps get recompiled or re-verified by dex2oat, the on-device compiler that turns DEX into optimized native code.

Zygote: why apps start quickly

Starting a fresh ART process and loading hundreds of framework classes for every app would be slow and waste RAM. Zygote ("the fertilized egg") solves this: it loads everything common once, then forks a copy for each new app. A fork shares memory pages with the parent copy-on-write, so ten apps share one copy of the framework. When you tap an icon:

  1. The launcher asks ActivityManager to start your Activity.
  2. If no process for your app exists, ActivityManager asks Zygote to fork one.
  3. The child process runs ActivityThread.main(), creates your Application, and finally your Activity (the cold start described in Android components).

Because every app is a child of Zygote, a framework class loaded in Zygote is shared and costs almost nothing per app.

Kotlin Boot sequence with a failing stage

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

From your code to an installed APK

The build tools turn source and resources into a signed package (the full picture is in Gradle for Android and Signing and release builds):

  1. Kotlin/Java compiler produces .class files.
  2. R8/D8 converts them to DEX (and shrinks and obfuscates in release builds).
  3. AAPT2 compiles resources and the manifest into a binary format and generates R.
  4. The APK builder packs DEX, resources, native libraries and the manifest into a zip.
  5. zipalign aligns uncompressed data on 4-byte boundaries so the OS can map it directly into memory.
  6. apksigner (the old jarsigner for v1) signs it. Modern schemes v2 to v4 sign the whole file.

In development you never run these by hand; adb install then sends the APK to the device. adb itself has three parts: the client you run, a server that listens on localhost TCP port 5037 and manages devices, and the daemon adbd on the phone. When adb devices finds nothing, restarting the server (adb kill-server, then adb start-server) is the first fix.

Garbage collection and memory leaks

You never free objects in Kotlin. The garbage collector (GC) finds objects nobody can reach and reclaims them. The core algorithm is mark and sweep:

  1. Mark: start from the GC roots (static fields, live thread stacks, active Activities' views) and follow every reference, marking each reachable object.
  2. Sweep: everything unmarked is garbage; its memory is freed.

The old Dalvik collector paused the app while it ran and did not compact memory, so free space became fragmented. ART improved this: its concurrent copying collector (default since Android 8) works while the app runs, and compacts by moving live objects together, and newer releases add generations (young objects are collected more often than old ones). The practical message is the same on every version: GC frees what is unreachable, so a leak is a reference you forgot to drop.

Kotlin Mark and sweep, with a leak

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

In the third run nothing is swept: the Activity is still reachable through the cache, so its whole view tree stays in memory. That is the typical Android leak: a static field, a singleton, a long-lived listener or a coroutine that holds an Activity Context after the screen closed. Prevent it by holding applicationContext instead of an Activity, clearing listeners and view bindings in onDestroyView, and cancelling work with the lifecycle. Android Studio's Memory Profiler and the LeakCanary library find these for you (see Performance).

When memory is truly short, Android does not only run the GC: its low-memory killer ends background processes in order of importance, which is why your app must save state (Saved state).

Common mistakes

  • Using the marketing version (14) where an API level (34) is needed.
  • Raising targetSdk without testing the behavior changes of every level in between.
  • Holding an Activity in a static field or singleton.
  • Assuming a first boot or first launch after an update is representative of normal speed.
  • Believing "the GC will clean it up" for objects that are still referenced.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which process is the parent of every app process on Android?

    Zygote preloads the framework and forks a child for each app, sharing memory copy-on-write. init starts Zygote; system_server is another child of it.
  2. 2. Which tool aligns uncompressed APK data on 4-byte boundaries?

    zipalign lets the OS memory-map data directly from the APK. aapt2 compiles resources; dex2oat compiles DEX to native code.
  3. 3. After a GC run, why would a finished Activity still be in memory?

    Mark and sweep frees only unreachable objects. A reference from a GC root, such as a static cache, keeps the Activity and its views alive.
  4. 4. What does adb use TCP port 5037 for?

    The adb client talks to the adb server on localhost:5037, which talks to adbd on each device.

Summary

  • Releases are identified by version, API level and (before 10) code name; code uses minSdk, targetSdk and compileSdk with API-level guards.
  • The stack is apps, framework, native libraries and ART, HAL, then the Linux kernel with Binder.
  • Boot goes Boot ROM, bootloader, kernel, init, Zygote, system_server, launcher, each stage verified.
  • Zygote forks every app so the framework is loaded once and shared.
  • An APK is built with compilers, D8/R8, AAPT2, zipalign and a signer; adb has a client, a server on port 5037 and a device daemon.
  • Garbage collection is mark and sweep from roots; leaks are references you did not drop.

Next: The Jetpack and AndroidX toolkit.