Skip to content

Add memory leak examples for Android documentation#920

Open
sudeshim3 wants to merge 6 commits into
mainfrom
feature/memory-leak-snippets
Open

Add memory leak examples for Android documentation#920
sudeshim3 wants to merge 6 commits into
mainfrom
feature/memory-leak-snippets

Conversation

@sudeshim3
Copy link
Copy Markdown
Collaborator

  • 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.

@sudeshim3 sudeshim3 requested review from a team, kkuan2011 and yrezgui as code owners May 14, 2026 17:20
@sudeshim3 sudeshim3 requested a review from dturner May 14, 2026 17:20
@snippet-bot
Copy link
Copy Markdown

snippet-bot Bot commented May 14, 2026

Here is the summary of changes.

You are about to add 11 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@sudeshim3 sudeshim3 requested a review from AliceYuan May 14, 2026 17:21
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread misc/src/main/java/com/example/snippets/memoryleaks/MemoryLeakSnippets.kt Outdated
* 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.
@sudeshim3 sudeshim3 force-pushed the feature/memory-leak-snippets branch from 671a754 to 8e3a6cc Compare May 14, 2026 17:25
Comment thread misc/src/main/java/com/example/snippets/memoryleaks/MemoryLeakSnippets.kt Outdated
Comment thread misc/src/main/java/com/example/snippets/memoryleaks/MemoryLeakSnippets.kt Outdated
Comment thread misc/src/main/java/com/example/snippets/memoryleaks/MemoryLeakSnippets.kt Outdated
Comment thread misc/src/main/java/com/example/snippets/memoryleaks/MemoryLeakSnippets.kt Outdated
Comment thread misc/src/main/java/com/example/snippets/memoryleaks/MemoryLeakSnippets.kt Outdated
Comment thread misc/src/main/java/com/example/snippets/memoryleaks/MemoryLeakSnippets.kt Outdated
* 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.
Copy link
Copy Markdown

@AliceYuan AliceYuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@sudeshim3 sudeshim3 force-pushed the feature/memory-leak-snippets branch from ffe85be to 535b120 Compare May 18, 2026 08:17
@yrezgui yrezgui enabled auto-merge (squash) May 18, 2026 11:31
Comment thread misc/src/main/java/com/example/snippets/memoryleaks/MemoryLeakSnippets.kt Outdated
* Replace AppCompatActivity with ComponentActivity in MainActivityWithLeak
  and MainActivityRecommended to make the snippets more Compose-friendly.
* Update copyright year in snippet files.
// [START android_compose_performance_memory_leak_location_recommended]
@Composable
fun LocationScreenRecommended(locationManager: LocationManager) {
var locationText by remember { mutableStateOf("Locating...") }
Copy link
Copy Markdown
Contributor

@raystatic raystatic May 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is recommended to use rememberUpdatedState for long-live operations.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sticking to the base remember keeps the example easy to read while still being technically correct. My goal was just to show the setup/teardown mechanism without getting bogged down in advanced Compose state mechanics. It will shift the the readers focus from 'how to clean up resources' to 'how Compose handles stale closures in lambdas'

var locationText by remember { mutableStateOf("Locating...") }

// DisposableEffect provides an onDispose block for mandatory cleanup
DisposableEffect(locationManager) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please check LifecycleStartEffect

It shows a location manager example only.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many platform-specific lifecycle handlers that developers should adopt based on their exact requirements. the primary focus of this document is on general memory leak patterns and bringing an understanding of how failing to clean up a UI component causes a leak. Introducing specific lifecycle APIs brings the focus to the example(Location Based app) instead of communicating the concept.
For example: LifecycleStartEffect aggressively tears down on onStop (when the app goes to the background), whereas DisposableEffect keeps a connection alive as long as the component is still in the UI tree. Whether you do it when the app goes to the background, or when your component is getting destroyed, is left for the developer to explore based on their exact needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants