Memory & battery

Leak detection (LeakCanary/Instruments), bitmap handling, wakelocks, background work limits (WorkManager, BGTaskScheduler). Battery drain gets apps uninstalled.

About 25 min read3-question knowledge check

The mental model

A phone is a shared apartment with a strict landlord and a metered electricity bill. Memory is the floor space: it's small, everyone's app shares it, and the moment the landlord (the OS) needs room, it evicts the tenants using the most space with the least justification — your backgrounded app is first out the door (Android's Low Memory Killer, iOS's jetsam). Battery is the electricity meter, and every time you wake the CPU, light up the radio, or grab the GPS, you spin it faster; the user sees the bill in Settings, attributes a drained phone to your app by name, and uninstalls. Unlike a server, nobody refills the RAM or plugs in the wall — you live inside hard, non-negotiable limits set by hardware and the OS.

The mental shift from server thinking: **on a server, a leak is a slow memory graph you restart weekly; on mobile, a leak is a crash and an uninstall.** A leaked Activity/ViewController doesn't just waste memory — it retains its entire view hierarchy and bitmaps, so a few leaked screens push you past the per-app limit. On Android that throws an OutOfMemoryError once the heap limit is hit, and under system-wide memory pressure the Low Memory Killer (Android) or jetsam (iOS) kills processes, which the user reads as "the app closed itself". And the OS actively fights your background work: Doze, App Standby Buckets, and background execution limits on Android, and the strict background-task budgets on iOS, exist specifically to stop apps from draining the battery while the user isn't looking. You don't get to run whenever you want; you get to ask the OS for a cheap moment.

The one idea that ties it together: the device is trying to kill your process and drain as little battery as possible on your behalf — your job is to hold as little memory as you can get away with and wake the hardware as rarely as possible. Every memory best practice is "release what you're not using so the killer skips you," and every battery best practice is "batch your work into fewer, cheaper wakeups."

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision