Add memory leak examples for Android documentation#920
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive set of code snippets demonstrating common memory leak patterns in Android and Compose, such as retained UI callbacks, improper lifecycle management for Flow collection, and uncleared view bindings. Feedback includes a recommendation to use repeatOnLifecycle for Flow collection in the Activity example to ensure efficiency and consistency with best practices. Additionally, it was suggested to re-categorize the fragment view binding example under Pattern 1 for improved documentation accuracy.
* Add Pattern 1: UI Objects Retained Beyond Lifecycle examples * Add Pattern 2: Background Work Outlives the UI examples * Add Pattern 3: Unreleased External Registrations examples * Include Compose and Fragment-specific memory management best practices * Ensure snippets are idiomatic and comply with Spotless formatting These examples illustrate common architectural pitfalls and their corresponding lifecycle-aware resolutions using Coroutines, Flows, and DisposableEffect.
671a754 to
8e3a6cc
Compare
* Refactor Repository examples to use actual ViewModel implementations instead of commented-out pseudo-code. * Split the recommended Repository callback snippet into pt1 and pt2. * Add explanatory comments to delay() calls clarifying their purpose to emulate network/processing.
AliceYuan
left a comment
There was a problem hiding this comment.
There's still an open question about removing the last view binding example but approving
* Align snippet structure and names with original document's retention chains. * Rename ImageLoader examples to ImagePickerRecommended and ImageCacheRecommended for clarity. * Replace delay() in delayed work examples with api.getUser() for realism. * Add missing Pattern 3 comment in Compose snippets file. * Use repeatOnLifecycle in Activity flow collection.
ffe85be to
535b120
Compare
| } | ||
| } | ||
|
|
||
| class MainActivityWithLeak : AppCompatActivity() { |
There was a problem hiding this comment.
can you make this ComponentActivity to make the example more compose-friendly? and below as well
| @@ -0,0 +1,72 @@ | |||
| /* | |||
| * Copyright 2024 The Android Open Source Project | |||
There was a problem hiding this comment.
nit: 2026 and same for next file too
These examples illustrate common architectural pitfalls and their corresponding lifecycle-aware resolutions using Coroutines, Flows, and DisposableEffect.