Android 应用运行时内存受限,系统在内存压力下会杀死应用。理解内存管理 — 特别是避免内存泄漏(Android 常见的严重问题)— 对于构建稳定、高性能的应用至关重要。
Android 内存基础
→ Apps have a LIMITED heap; the system can KILL background apps to reclaim memory
→ Garbage collection reclaims unreachable objects — but GC pauses can cause jank, and
objects that STAY reachable (leaks) are never collected
→ Low-memory devices are common (Android's range) → memory efficiency matters
内存泄漏 — 常见问题
A LEAK = an object that's no longer needed but is still REFERENCED (so GC can't free it).
Classic Android leaks:
✗ Holding an ACTIVITY/Context reference beyond its lifecycle:
- a static field or singleton holding an Activity/Context
- a long-lived listener/callback capturing an Activity
- an inner class / non-cancelled coroutine / handler holding the Activity
→ the Activity can't be GC'd after it's destroyed → its whole view tree leaks (big!)
