Skip to content

fix: Resolve memory leak in image_list example - #189526

Merged
auto-submit[bot] merged 8 commits into
flutter:masterfrom
Okikday:fix-image-list-leak
Nov 3, 2026
Merged

auto-submit[bot] merged 8 commits into
flutter:masterfrom
Okikday:fix-image-list-leak

Conversation

@Okikday

@Okikday Okikday commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

This PR resolves a memory leak and cleans up naming conventions in the image_list Devicelab benchmark example.

What is changing:

  • Moved the AnimationController instantiation out of the build method and into a late final variable to prevent redundant instantiation.
  • Added a dispose() method to properly clean up the controllers.

Why:
Previously, 50 new controllers were being instantiated on every setState trigger without ever being disposed, causing a memory leak. This refactor correctly manages the widget lifecycle while preserving the underlying performance benchmark (by appending ..repeat() to the initialization so the UI thread remains appropriately stressed).

This PR fixes a trivial lifecycle and styling issue in a devicelab test script. No open issue is associated.

Pre-launch Checklist

@flutter-dashboard

Copy link
Copy Markdown

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@google-cla

google-cla Bot commented Jul 15, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot added the d: examples Sample code and demos label Jul 15, 2026

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

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.

Code Review

This pull request refactors the image_list example by renaming variables for style consistency and moving the initialization of AnimationControllers from the build method to a class-level field, adding a dispose method to clean them up. Feedback recommends initializing the controllers in initState instead of using a late final field to prevent unnecessary resource allocation during lazy initialization in dispose, and renaming the field to _controllers to follow Dart private naming conventions.

Comment thread examples/image_list/lib/main.dart Outdated
Comment on lines +151 to +165
late final controllers = <AnimationController>[
for (int i = 0; i < imagesCount; i++)
AnimationController(
duration: const Duration(milliseconds: 3600),
vsync: this,
)..repeat(),
];

@override
void dispose() {
for (final controller in controllers) {
controller.dispose();
}
super.dispose();
}

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.

medium

Using a late final field initializer for controllers can lead to unnecessary resource allocation during widget disposal. If the widget is disposed before it is ever built (e.g., in certain test scenarios or rapid navigation), accessing controllers in dispose() will trigger its lazy initialization. This creates and starts 50 AnimationControllers only to immediately dispose of them.

Additionally, to conform to Dart private naming conventions (consistent with _counter), the field should be prefixed with an underscore (_controllers).

It is highly recommended to initialize the controllers in initState() instead.

  late final List<AnimationController> _controllers;

  @override
  void initState() {
    super.initState();
    _controllers = <AnimationController>[
      for (int i = 0; i < imagesCount; i++)
        AnimationController(
          duration: const Duration(milliseconds: 3600),
          vsync: this,
        )..repeat(),
    ];
  }

  @override
  void dispose() {
    for (final controller in _controllers) {
      controller.dispose();
    }
    super.dispose();
  }
References
  1. Effective Dart: Style recommends prefixing private identifiers with an underscore. (link)

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 address this feedback and add a test to validate the memory leak has been fixed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure. I will do that in a moment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can you address this feedback and add a test to validate the memory leak has been fixed?

Done!. Added the tests to validate that no memory leaks, and also fixed the one line lint warning I missed earlier after running 'flutter analyze'

Comment thread examples/image_list/lib/main.dart Resolved
@Piinks
Piinks self-requested a review October 26, 2026 21:44
Comment thread examples/image_list/lib/main.dart Outdated
final ByteData byteData = await rootBundle.load('images/coast.jpg');
httpServer.listen((HttpRequest request) async {
const chunk_size = 2048;
const chunkSize = 2048;

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 remove the changes here unrelated to the actual bug fix, like changing variable names?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, I can do just that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can you remove the changes here unrelated to the actual bug fix, like changing variable names?

Done!

@flutter-dashboard

Copy link
Copy Markdown

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If both you and the PR author believe this PR qualifies for a test exemption, escalate to the tech lead for the changed area to review and apply the test-exempt label. All reviewers should feel empowered to ask for tests before escalating.

@Piinks Piinks left a comment

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.

Thanks for addressing the review feedback and adding the widget test! This needs dart format run on examples/image_list/lib/main.dart and examples/image_list/test/widget_test.dart.


void main() {
testWidgets('MyHomePage disposes AnimationControllers properly', (WidgetTester tester) async {
HttpOverrides.global = _FakeHttpOverrides();

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: Consider resetting HttpOverrides.global in addTearDown so global state doesn't leak if additional tests are added to this file later

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nit: Consider resetting HttpOverrides.global in addTearDown so global state doesn't leak if additional tests are added to this file later

And done.
Thanks for the review!

Piinks
Piinks previously approved these changes Oct 30, 2026

@Piinks Piinks left a comment

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.

LGTM

@Piinks Piinks added the CICD Run CI/CD label Oct 30, 2026
AbdeMohlbi
AbdeMohlbi previously approved these changes Oct 30, 2026
- Moved AnimationController initialization to a late final variable to prevent redundant instantiation.
- Added dispose() method to properly clean up the controllers.
- Renamed images to imagesCount to conform to Dart naming conventions.
…ist and fix lint warnings (type of local variable was non-obvious)
@Piinks
Piinks force-pushed the fix-image-list-leak branch from da63d9a to 7da0f16 Compare October 30, 2026 21:15
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 30, 2026
@Piinks Piinks added CICD Run CI/CD autosubmit Merge PR when tree becomes green via auto submit App labels Oct 30, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 30, 2026
@auto-submit

auto-submit Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/189526, because - The status or check suite Tree_analyze has failed. Please fix the issues identified (or deflake) before re-applying this label.

@Piinks

Piinks commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

@Okikday can you address the failing checks here?

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 31, 2026
@Okikday
Okikday dismissed stale reviews from AbdeMohlbi and Piinks via af4a9c8 October 31, 2026 06:15
@Okikday

Okikday commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

@Okikday can you address the failing checks here?

I have pushed a fix for the cross-import check (used widgets.dart instead of Material import and found the button by tooltip). So could you add the CICD label so CI re-runs? Thanks!

@AbdeMohlbi AbdeMohlbi added the CICD Run CI/CD label Oct 31, 2026
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 31, 2026
@Okikday

Okikday commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for running the Tree analyzer workflow @AbdeMohlbi
I’ve formatted the widget test that was causing the failure. Could someone re-add the CICD label so CI re-runs?

@AbdeMohlbi AbdeMohlbi added the CICD Run CI/CD label Oct 31, 2026

@Piinks Piinks left a comment

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.

LGTM

@AbdeMohlbi AbdeMohlbi added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 2, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 2, 2026
@auto-submit

auto-submit Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/189526, because This PR has not met approval requirements for merging. The PR author is not a member of flutter-hackers and needs 1 more review(s) in order to merge this PR.

  • Merge guidelines: A PR needs at least one approved review if the author is already part of flutter-hackers or two member reviews if the author is not a member of flutter-hackers before re-applying the autosubmit label. Reviewers: If you left a comment approving, please use the "approve" review action instead.

@AbdeMohlbi AbdeMohlbi added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 2, 2026
@AbdeMohlbi

Copy link
Copy Markdown
Member

autosubmit label was removed for flutter/flutter/189526, because This PR has not met approval requirements for merging. The PR author is not a member of flutter-hackers and needs 1 more review(s) in order to merge this PR.

  • Merge guidelines: A PR needs at least one approved review if the author is already part of flutter-hackers or two member reviews if the author is not a member of flutter-hackers before re-applying the autosubmit label. Reviewers: If you left a comment approving, please use the "approve" review action instead.

yuh uh

@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 2, 2026
Merged via the queue into flutter:master with commit 3f07d59 Nov 3, 2026
35 of 36 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 3, 2026
gmackall pushed a commit to gmackall/flutter that referenced this pull request Nov 3, 2026
This PR resolves a memory leak and cleans up naming conventions in the
`image_list` Devicelab benchmark example.

**What is changing:**
- Moved the `AnimationController` instantiation out of the `build`
method and into a `late final` variable to prevent redundant
instantiation.
- Added a `dispose()` method to properly clean up the controllers.

**Why:**
Previously, 50 new controllers were being instantiated on every
`setState` trigger without ever being disposed, causing a memory leak.
This refactor correctly manages the widget lifecycle while preserving
the underlying performance benchmark (by appending `..repeat()` to the
initialization so the UI thread remains appropriately stressed).

*This PR fixes a trivial lifecycle and styling issue in a devicelab test
script. No open issue is associated.*

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 3, 2026
Roll Flutter from fcfb4f203eae to 934720261be0 (38 revisions)

flutter/flutter@fcfb4f2...9347202

2026-11-03 [email protected] Roll Skia from 5bceaa033999 to c733ad3e37cb (5 revisions) (flutter/flutter#195872)
2026-11-03 [email protected] Roll Skia from 34d2bf479385 to 5bceaa033999 (9 revisions) (flutter/flutter#195864)
2026-11-03 [email protected] fix: prefer pointer events over semantics tap for mouse clicks on web (flutter/flutter#191529)
2026-11-03 [email protected] [VPAT] Expose a semantics text field value while it is not being edited (flutter/flutter#195445)
2026-11-03 [email protected] [Impeller] Make a GL context current before constructing an AiksContext without a surface (flutter/flutter#195629)
2026-11-03 [email protected] [flutter_tools] Eliminate globals.dart from project.dart (flutter/flutter#194066)
2026-11-03 [email protected] [tool] Complete context runner elimination and migrate AppContext usages to ToolContext (flutter/flutter#190797)
2026-11-03 [email protected] [web] Unify font collections between CanvasKit and Skwasm (flutter/flutter#195539)
2026-11-03 [email protected] [Android] Replay predictive back state for retained engines (flutter/flutter#192286)
2026-11-03 [email protected] [vulkan]: Adds depth/stencil synchronization for subpasses (flutter/flutter#195689)
2026-11-03 [email protected] [web] Honor requested upscaling in instantiateImageCodecWithSize and ImageDescriptor.instantiateCodec (flutter/flutter#195836)
2026-11-03 [email protected] Roll Skia from 866b61ca92c9 to 34d2bf479385 (9 revisions) (flutter/flutter#195848)
2026-11-03 [email protected] Roll Fuchsia Test Scripts from xl1LWBuppzu-BnOzV... to B6zarykZp6PKWRueg... (flutter/flutter#195847)
2026-11-03 [email protected] Remove unused ffi (flutter/flutter#195710)
2026-11-02 [email protected] Revert: [Android] Let the raster thread use every core of a chip without little cores (flutter/flutter#195838)
2026-11-02 [email protected] Run 10 impeller iOS benchmarks as one devicelab task (flutter/flutter#195661)
2026-11-02 [email protected] Use named uniforms instead of setFloat in StretchEffect (flutter/flutter#195372)
2026-11-02 [email protected] [flutter_tools] Report when the browser disconnects during a web test run (flutter/flutter#192198)
2026-11-02 [email protected] fix: Resolve memory leak in image_list example (flutter/flutter#189526)
2026-11-02 [email protected] Only run the common local engine configurations in presubmit (flutter/flutter#195688)
2026-11-02 [email protected] Update gcloud_version to version [email protected] (flutter/flutter#195769)
2026-11-02 [email protected] [Impeller] Order the Vulkan copy-texture-to-texture layout transition after the copy (flutter/flutter#195814)
2026-11-02 [email protected] [Impeller] Order the Vulkan ResizeTexture layout transition after its blit (flutter/flutter#195721)
2026-11-02 [email protected] [web] Fix CanvasKit fragment shader uniforms being freed or mutated after recording (flutter/flutter#195718)
2026-11-02 [email protected] Roll Fuchsia Linux SDK from MqypI6RNyVZ_FLUE-... to T_-6-6X1lDMghhAvX... (flutter/flutter#195809)
2026-11-02 [email protected] [Android] Log why Impeller falls back to OpenGLES (flutter/flutter#194764)
2026-11-02 [email protected] Roll Skia from 3f35fb6e591b to 866b61ca92c9 (11 revisions) (flutter/flutter#195802)
2026-11-02 [email protected] Roll Skia from 3f35fb6e591b to 866b61ca92c9 (11 revisions) (flutter/flutter#195802)
2026-11-02 [email protected] Only run the common local engine configurations in presubmit (flutter/flutter#195688)
2026-11-02 [email protected] Balance tool_integration_tests subshards by expected duration (flutter/flutter#195628)
2026-11-02 [email protected] [web_paragraph] Map code units without a cluster to a cluster (flutter/flutter#195384)
2026-11-02 [email protected] [web] Hide synthetic autofill inputs from assistive technology (flutter/flutter#195618)
2026-11-02 [email protected] Roll Packages from 3742a4e to 3f34a6f (8 revisions) (flutter/flutter#195795)
2026-11-02 [email protected] [web] Unskip web tests that pass on CanvasKit and Skwasm: platform_view_test, route_notification_messages_test (flutter/flutter#195605)
2026-11-02 [email protected] Roll Skia from e5ad3868b8db to 3f35fb6e591b (1 revision) (flutter/flutter#195789)
2026-11-02 [email protected] Roll Skia from f5215e74aa16 to e5ad3868b8db (3 revisions) (flutter/flutter#195779)
2026-11-02 [email protected] Roll Skia from e43478a2f9f8 to f5215e74aa16 (1 revision) (flutter/flutter#195776)
2026-11-01 [email protected] Roll Fuchsia Linux SDK from kW0A0xxNkqf38ljm4... to MqypI6RNyVZ_FLUE-... (flutter/flutter#195768)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD d: examples Sample code and demos

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants