Skip to content

Add memory leak examples for Android documentation#920

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

Add memory leak examples for Android documentation#920
sudeshim3 wants to merge 5 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
}
}

class MainActivityWithLeak : AppCompatActivity() {
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.

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

nit: 2026 and same for next file too

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.

4 participants