leak-tracker: Support pure Dart package
Hi,
leak-tracker is an important tool in the dart world as the language is moving from being mainly front to also model rich world logic.
I'm working on a large package that would greatly benefits from memory insights while dealing with deep data structures and operations.
Thanks!
Thanks for filing it! How about design discussion? I sent you message in Twitter to schedule.
Relevant issue: https://github.com/flutter/flutter/issues/161132
Question: can you add dev dependency on flutter_test and use 'testWidgets' instead of 'test' to get leak tracking for your tests? If this works out, we just need to document this.
It should be possible but it require to reconfigure the project from pure dart to flutter as we also need to import the flutter sdk.
Not the entire project, but tests only. And another option is to have separate flutter project with just tests. But, yes, ideally we want to update the pure dart method 'test' to support leak tracking. First step would be to create one page design doc (template, example) and review it with team.
Currently I'm using a monorepo with all logics and tests relying on dart then another flutter project build on the logic. To use flutter test on logic would mean to import Flutter sdk which is not an option for later build purpose.
Flutter projects would not cover the same testing surface thus a case to check for memory leaks first on a pure dart project and then later on widgets.
Currently I'm using a monorepo with all logics and tests relying on dart then another flutter project build on the logic. To use flutter test on logic would mean to import Flutter sdk which is not an option for later build purpose.
Flutter projects would not cover the same testing surface thus a case to check for memory leaks first on a pure dart project and then later on widgets.
Makes sense. Then next step is a one-page design doc to enable leak tracking in dart test.
Hi!
Since Dart server side is gaining traction this feature is more welcome than ever when dependencies land. I'll be patient :)
Thanks
Hi!
Since Dart server side is gaining traction this feature is more welcome than ever when dependencies land. I'll be patient :)
Thanks
Thank you!
@vsmenon, I am not sure who in Dash is handling priorities for server side memory efficiency. Can you help to navigate this feedback to a right channel?
I've been working on an intensive desktop app and beyond relying kinda blindly on dispose and some GC interprerations, it's difficult to find side mistakes.
Hope for this tool to help the delivery more solid for long term session.
To get attention of Dart test people, it would make sense to file a new issue towards dart test, requesting to integrated test with leak tracker the same way as testWidgets is integrated: https://github.com/dart-lang/test/issues
@polina-c do you think there is a way to setup leak tracking in Dart tests without direct support from the test package?
Maybe there is something we could put in our setUp/tearDown methods?
I tried that but without success
import 'package:leak_tracker/leak_tracker.dart';
void main() {
setUp(() {
LeakTracking.start();
});
tearDown(() async {
LeakTracking.declareNotDisposedObjectsAsLeaks();
final leaks = await LeakTracking.collectLeaks();
LeakTracking.stop();
print("${leaks.all}");
});
Do you see something missing?
It is interesting suggestion.
At first glance I do not see why it would not work. You have not-disposed objects in your tests, right?
If I write this:
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
void main() {
LeakTesting.enable();
testWidgets('Check leak', (tester) async {
var notifier = ValueNotifier(3);
});
}
And run with flutter test test/my_flutter_test.dart I got the error as expected: Expected: leak free .. Which: contains leaks: ... ValueNotifier<int>
If I write this:
import 'package:flutter/foundation.dart';
import 'package:test/test.dart';
import 'package:leak_tracker/leak_tracker.dart';
void main() {
setUp(() {
LeakTracking.start();
});
tearDown(() async {
LeakTracking.declareNotDisposedObjectsAsLeaks();
final leaks = await LeakTracking.collectLeaks();
LeakTracking.stop();
print("${leaks.all}");
});
test('Check leak', () async {
var notifier = ValueNotifier(3);
});
}
And run with the same command flutter test test/my_dart_test.dart. leaks object is empty.
Thanks for the details.
ValueNotifier is a Flutter object that reports activity (creation and disposal) to FlutterMemoryAllocations, not to leak tracker directly (to see this, check code of ValueNotifier and ChangeNotifier). To connect this activity to leak tracker, you need to run this before starting leak tracker:
FlutterMemoryAllocations.instance.addListener(
(ObjectEvent event) => LeakTracking.dispatchObjectEvent(event.toMap()),
);
And, you want to replace setup with setupAll, and tearDown with tearDownAll, because leak collection is a heavy operation, that you do not want to happen after every test.
Hope it helps.