Coming soon! See flutter/devtools#3951.
The text below is under construction.
The documentation below is valid for Flutter SDKs >= 3.18.0.
This page describes how to auto-detect certain types of memory leaks. Read more about leak tracking in overview.
The Flutter test method testWidgets
can be configured to track and detect leaks
from all instrumented classes. To enable leak tracking for your entire test suite
and to configure leak tracking settings:
- Add dev_dependency on
leak_tracker_flutter_testing
. Putany
instead of version, because the version is defined by your Flutter SDK.
dev_dependencies:
...
leak_tracker_flutter_testing: any
- Add or modify your
test/flutter_test_config.dart
file:
import 'dart:async';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
...
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
LeakTesting.enable();
LeakTesting.settings = LeakTesting.settings
.withIgnored(createdByTestHelpers: true);
...
await testMain();
}
You can also adjust leak tracking settings for individual tests:
testWidgets('Images are rendered as expected.',
// TODO(polina-c): make sure images are disposed, https://github.com/polina-c/my_repo/issues/141
experimentalLeakTesting: LeakTesting.settings.withIgnored(classes: ['Image']),
(WidgetTester tester) async {
...
See documentation for testWidgets
for more information.
To instrument a disposable class for leak tracking, you need to dispatch object creation and disposal events to leak_tracker. Use the example as a guide.
TODO(polina-c): implement and test this scenario #172
-
Add leak_tracker to
dependencies
inpubspec.yaml
. -
Before
runApp
invocation, enable leak tracking, and connect the Flutter memory allocation events:
import 'package:flutter/foundation.dart';
import 'package:leak_tracker/leak_tracker.dart';
...
enableLeakTracking();
FlutterMemoryAllocations.instance
.addListener((ObjectEvent event) => dispatchObjectEvent(event.toMap()));
runApp(...);
- Run the application in debug mode and watch for a leak related warnings.
TODO(polina-c): add example of the warning #172
At this time, leak tracking is supported only for unit tests within Flutter packages.
With the latest version of leak_tracker, leak tracking is not supported for:
- Web platform
- Running applications
- Pure Dart packages
Leak tracking for not-GCed leaks is experimental and is off by default. At this time, it is recommended to track only not-disposed leaks.
The leak_tracker will catch leaks only for instrumented objects (see concepts for details).
However, the good news is:
-
Disposables in the Flutter Framework are instrumented. If your Flutter app manages widgets in a way that results in leaks,
leak_tracker
will catch them. -
If a leak involves at least one instrumented object, the leak will be caught and all other objects, even non-instrumented, will stop leaking as well.
See the instrumentation guidance.
The leak_tracker availability differs by build modes. See Dart build modes or Flutter build modes.
Dart development and Flutter debug
Leak tracking is fully available.
Flutter profile
Leak tracking is available, but FlutterMemoryAllocations
that listens to
Flutter instrumented objects,
should be turned on
if you want to track Flutter Framework objects.
Dart production and Flutter release
Leak tracking is disabled.
NOTE: If you are interested in enabling leak tracking for release mode, please, comment here.
The leak_tracker stores a small additional record for each tracked alive object and for each detected leak, that increases the memory footprint.
For the Gallery application
in profile mode on macos
the leak tracking increased memory footprint of the home page
by ~400 KB that is ~0.5% of
the total.
Leak tracking impacts CPU in two areas: