leak_tracker icon indicating copy to clipboard operation
leak_tracker copied to clipboard

leak-tracker: Support pure Dart package

Open Solido opened this issue 1 year ago • 15 comments

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!

Solido avatar Dec 02 '24 19:12 Solido

Thanks for filing it! How about design discussion? I sent you message in Twitter to schedule.

polina-c avatar Dec 06 '24 01:12 polina-c

Relevant issue: https://github.com/flutter/flutter/issues/161132

polina-c avatar Jan 05 '25 06:01 polina-c

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.

polina-c avatar Jan 05 '25 06:01 polina-c

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.

Solido avatar Jan 05 '25 09:01 Solido

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.

polina-c avatar Jan 05 '25 18:01 polina-c

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.

Solido avatar Jan 05 '25 19:01 Solido

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.

polina-c avatar Jan 05 '25 19:01 polina-c

Hi!

Since Dart server side is gaining traction this feature is more welcome than ever when dependencies land. I'll be patient :)

Thanks

Solido avatar Sep 01 '25 14:09 Solido

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?

polina-c avatar Sep 03 '25 20:09 polina-c

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.

Solido avatar Sep 03 '25 22:09 Solido

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 avatar Sep 04 '25 18:09 polina-c

@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?

xvrh avatar Sep 19 '25 07:09 xvrh

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?

polina-c avatar Sep 19 '25 13:09 polina-c

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.

xvrh avatar Sep 19 '25 14:09 xvrh

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.

polina-c avatar Sep 19 '25 14:09 polina-c