Android多次旋转应用后Activity多实例是否为内存泄漏?
Great question—this is a super common point of confusion when debugging Android memory issues, and your initial hunch is correct: this is not a memory leak. Let me break down why you're seeing this behavior:
GC isn't instantaneous: When you rotate your device, Android destroys the old Activity instance and creates a new one. However, the old instance doesn't get immediately garbage collected. The Android runtime only triggers GC when it detects sufficient memory pressure, not right after an object becomes unreachable. So when you take a memory dump without manually triggering GC first, those old Activity instances are still sitting in memory, waiting to be cleaned up—but they don't have any active strong references keeping them alive.
Manual GC confirms no leak: When you trigger a forced GC via Android Studio, those unreachable old Activity instances are immediately cleaned up, leaving only the current, active Activity. This is the key sign that there's no leak: if there were a persistent strong reference (like a static variable, an unbounded listener, or a background task holding onto the Activity), the old instances would still remain in memory even after GC.
To clarify the critical difference:
- A memory leak occurs when an object that's no longer needed is kept alive by an unintended strong reference. These objects will never be GC'd, even under memory pressure, leading to gradual memory growth and potential OOM crashes.
- Uncollected but unreachable objects (like your old Activity instances pre-GC) are just waiting for the runtime to get around to cleaning them up—this is normal behavior, not a leak.
If you want to be 100% confident, you can use tools like LeakCanary: it automatically detects leaks by waiting to see if objects are still present after multiple GC cycles. If it doesn't flag your Activity as leaked, you can rest easy.
内容的提问来源于stack exchange,提问作者Lisa Anne

