Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance. #98

Merged
merged 7 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 5 additions & 70 deletions pkgs/leak_tracker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 8.0.2

* Improve performance.
* Make gcCountBuffer customizable with default value 3.

# 8.0.1

* Handle SentinelException for retaining path.
Expand All @@ -11,51 +16,24 @@
# 7.0.8

* Disconnect from service after obtaining retaining paths.

# 7.0.7

* Protect from identityHashCode equal to 0.

# 7.0.6

* Add helpers for troubleshooting.
* Handle generic arguments for retaining path detection.

# 7.0.5

* Convert to multi-package.

# 7.0.4

* Fix path collection.
* Create constructor to collect path.

# 7.0.3

* Fix connection issue.

# 7.0.2

* Improve retaining path formatting.

# 7.0.1

* Format retaining path nicely.

# 7.0.0

* Enable collection of retaining path.

# 6.0.3

* Separate testing.

# 6.0.2

* Fixes to support g3.

# 6.0.1

* Fix for MemoryUsageEvent constructor.

# 6.0.0
Expand Down Expand Up @@ -98,13 +76,6 @@
* Breaking changes: update names of types to be align with Flutter naming convention.
* Add model for Flutter unit testing configuration.
* Adopt Flutter standard lints.

# 2.0.3

* Improve more documentation.

# 2.0.2

* Improve documentation.

# 2.0.1
Expand All @@ -115,45 +86,9 @@
# 2.0.0

* Breaking changes in `withLeakTracking`.

# 1.0.2

* Refactor test_infra libraries.

# 1.0.1

* Documentation updates.

# 1.0.0

* First release.

# 1.0.0-dev.1.6

* Enable reset for leak tracker.

# 1.0.0-dev.1.5

* Define matcher for leaks.

# 1.0.0-dev.1.4

* Tests and minor fixes.

# 1.0.0-dev.1.3

* Test for `pumpWidget`.
* Make `disposalTimeBuffer` configurable.
* Testing API `withLeakTracking`.

# 1.0.0-dev.1.2

* Minor fixes.

# 1.0.0-dev.1.1

* Reorganize example to fit pub.dev standards.

# 1.0.0-dev.1.0

* Create initial structure.
7 changes: 1 addition & 6 deletions pkgs/leak_tracker/lib/src/leak_tracking/_gc_counter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ class GcCounter {
int get gcCount => reachabilityBarrier;
}

/// Delta of GC cycles, enough for a non reachable object to be GCed.
///
/// Theoretically, 2 should be enough, however it gives false positives
/// if there is no activity in the application for ~5 minutes.
const gcCountBuffer = 3;

/// True, if the disposed object is expected to be GCed,
/// assuming at the disposal moment it was referenced only
/// by the the disposal invoker.
Expand All @@ -25,6 +19,7 @@ bool shouldObjectBeGced({
required int currentGcCount,
required DateTime currentTime,
required Duration disposalTimeBuffer,
required int gcCountBuffer,
}) =>
currentGcCount - gcCountAtDisposal >= gcCountBuffer &&
currentTime.difference(timeAtDisposal) >= disposalTimeBuffer;
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class ObjectRecord {
bool get isGCed => _gcedGcCount != null;
bool get isDisposed => _disposalGcCount != null;

bool isGCedLateLeak(Duration disposalTimeBuffer) {
bool isGCedLateLeak(Duration disposalTimeBuffer, int gcCountBuffer) {
if (_disposalGcCount == null || _gcedGcCount == null) return false;
assert(_gcedTime != null);
return shouldObjectBeGced(
Expand All @@ -125,13 +125,15 @@ class ObjectRecord {
currentGcCount: _gcedGcCount!,
currentTime: _gcedTime!,
disposalTimeBuffer: disposalTimeBuffer,
gcCountBuffer: gcCountBuffer,
);
}

bool isNotGCedLeak(
int currentGcCount,
DateTime currentTime,
Duration disposalTimeBuffer,
int gcCountBuffer,
) {
if (_gcedGcCount != null) return false;
return shouldObjectBeGced(
Expand All @@ -140,6 +142,7 @@ class ObjectRecord {
currentGcCount: currentGcCount,
currentTime: currentTime,
disposalTimeBuffer: disposalTimeBuffer,
gcCountBuffer: gcCountBuffer,
);
}

Expand Down
13 changes: 10 additions & 3 deletions pkgs/leak_tracker/lib/src/leak_tracking/_object_tracker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ObjectTracker implements LeakProvider {
ObjectTracker({
this.leakDiagnosticConfig = const LeakDiagnosticConfig(),
required this.disposalTimeBuffer,
required this.gcCountBuffer,
FinalizerBuilder? finalizerBuilder,
GcCounter? gcCounter,
IdentityHashCoder? coder,
Expand All @@ -51,6 +52,8 @@ class ObjectTracker implements LeakProvider {

final LeakDiagnosticConfig leakDiagnosticConfig;

final int gcCountBuffer;

void startTracking(
Object object, {
required Map<String, dynamic>? context,
Expand Down Expand Up @@ -90,7 +93,7 @@ class ObjectTracker implements LeakProvider {
final record = _notGCed(code);
record.setGCed(_gcCounter.gcCount, clock.now());

if (record.isGCedLateLeak(disposalTimeBuffer)) {
if (record.isGCedLateLeak(disposalTimeBuffer, gcCountBuffer)) {
_objects.gcedLateLeaks.add(record);
} else if (record.isNotDisposedLeak) {
_objects.gcedNotDisposedLeaks.add(record);
Expand Down Expand Up @@ -175,8 +178,12 @@ class ObjectTracker implements LeakProvider {

final now = clock.now();
for (int code in _objects.notGCedDisposedOk.toList(growable: false)) {
if (_notGCed(code)
.isNotGCedLeak(_gcCounter.gcCount, now, disposalTimeBuffer)) {
if (_notGCed(code).isNotGCedLeak(
_gcCounter.gcCount,
now,
disposalTimeBuffer,
gcCountBuffer,
)) {
_objects.notGCedDisposedOk.remove(code);
_objects.notGCedDisposedLate.add(code);
objectsToGetPath?.add(code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void enableLeakTracking({
final newTracker = ObjectTracker(
leakDiagnosticConfig: theConfig.leakDiagnosticConfig,
disposalTimeBuffer: theConfig.disposalTimeBuffer,
gcCountBuffer: theConfig.gcCountBuffer,
);

_objectTracker.value = newTracker;
Expand Down
18 changes: 15 additions & 3 deletions pkgs/leak_tracker/lib/src/leak_tracking/leak_tracker_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ class LeakDiagnosticConfig {
classesToCollectStackTraceOnDisposal.contains(classname);
}

/// The default value for number of full GC cycles, enough for a non reachable object to be GCed.
///
/// It is pessimistic assuming that user will want to
/// detect leaks not more often than a second.
///
/// Theoretically, 2 should be enough, however it gives false positives
/// if there is no activity in the application for ~5 minutes.
const defaultGcCountBuffer = 3;
polina-c marked this conversation as resolved.
Show resolved Hide resolved

class LeakTrackingConfiguration {
const LeakTrackingConfiguration({
this.stdoutLeaks = true,
Expand All @@ -86,6 +95,7 @@ class LeakTrackingConfiguration {
this.checkPeriod = const Duration(seconds: 1),
this.disposalTimeBuffer = const Duration(milliseconds: 100),
this.leakDiagnosticConfig = const LeakDiagnosticConfig(),
this.gcCountBuffer = defaultGcCountBuffer,
polina-c marked this conversation as resolved.
Show resolved Hide resolved
});

/// The leak tracker:
Expand All @@ -95,14 +105,19 @@ class LeakTrackingConfiguration {
/// at the moment of leak checking.
LeakTrackingConfiguration.passive({
LeakDiagnosticConfig leakDiagnosticConfig = const LeakDiagnosticConfig(),
int gcCountBuffer = defaultGcCountBuffer,
}) : this(
stdoutLeaks: false,
notifyDevTools: false,
checkPeriod: null,
disposalTimeBuffer: const Duration(),
leakDiagnosticConfig: leakDiagnosticConfig,
gcCountBuffer: gcCountBuffer,
);

/// Number of full GC cycles, enough for a non reachable object to be GCed.
final int gcCountBuffer;

final LeakDiagnosticConfig leakDiagnosticConfig;

/// Period to check for leaks.
Expand All @@ -120,9 +135,6 @@ class LeakTrackingConfiguration {
final LeakSummaryCallback? onLeaks;

/// Time to allow the disposal invoker to release the reference to the object.
///
/// The default value is pessimistic assuming that user will want to
/// detect leaks not more often than a second.
final Duration disposalTimeBuffer;
}

Expand Down
11 changes: 6 additions & 5 deletions pkgs/leak_tracker/lib/src/leak_tracking/orchestration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:developer';

import '../shared/shared_model.dart';
import '_formatting.dart';
import '_gc_counter.dart';
import 'leak_tracker.dart';
import 'leak_tracker_model.dart';
import 'retaining_path/_connection.dart';
Expand Down Expand Up @@ -42,6 +41,9 @@ class MemoryLeaksDetectedError extends StateError {
/// to wait infinitely for the forced garbage collection, that is needed
/// to analyse results.
///
/// [gcCountBuffer] is number of full GC cycles, enough for a non reachable object to be GCed.
///
///
/// If you test Flutter widgets, connect their instrumentation to the leak
/// tracker:
/// ```
Expand Down Expand Up @@ -76,6 +78,7 @@ Future<Leaks> withLeakTracking(
Duration? timeoutForFinalGarbageCollection,
LeakDiagnosticConfig leakDiagnosticConfig = const LeakDiagnosticConfig(),
AsyncCodeRunner? asyncCodeRunner,
int gcCountBuffer = defaultGcCountBuffer,
}) async {
if (callback == null) return Leaks({});

Expand Down Expand Up @@ -144,12 +147,10 @@ Future<void> forceGC({
final Stopwatch? stopwatch = timeout == null ? null : (Stopwatch()..start());
final int barrier = reachabilityBarrier;

final List<List<DateTime>> storage = <List<DateTime>>[];
final List<List<int>> storage = <List<int>>[];

void allocateMemory() {
storage.add(
Iterable<DateTime>.generate(10000, (_) => DateTime.now()).toList(),
);
storage.add(List.generate(30000, (n) => n));
if (storage.length > 100) {
storage.removeAt(0);
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/leak_tracker/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: leak_tracker
version: 8.0.1
version: 8.0.2
description: A framework for memory leak tracking for Dart and Flutter applications.
repository: https://github.com/dart-lang/leak_tracker/tree/main/pkgs/leak_tracker

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';

import 'package:leak_tracker/leak_tracker.dart';
import 'package:test/test.dart';

Expand All @@ -18,4 +20,35 @@ void main() {
final path = await formattedRetainingPath(myObject.ref);
expect(path, contains('_test.dart/MyClass:stopwatch'));
});

group('forceGC', () {
test('forces gc', () async {
Object? myObject = <int>[1, 2, 3, 4, 5];
final ref = WeakReference(myObject);
myObject = null;

await forceGC();

expect(ref.target, null);
});

test('times out', () async {
await expectLater(
() async => forceGC(timeout: Duration.zero),
throwsA(isA<TimeoutException>()),
);
});

test('takes reasonable time', () async {
const rounds = 100;
final sw = Stopwatch()..start();

for (var _ in Iterable.generate(rounds)) {
await forceGC();
}

final durationPerRound = sw.elapsed ~/ rounds;
expect(durationPerRound.inMilliseconds, lessThan(100));
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:leak_tracker/leak_tracker.dart';
import 'package:leak_tracker/src/leak_tracking/_gc_counter.dart';
import 'package:test/test.dart';

Expand All @@ -18,6 +19,7 @@ void main() {
currentGcCount: gcNow,
currentTime: now,
disposalTimeBuffer: disposalTimeBuffer,
gcCountBuffer: defaultGcCountBuffer,
);

final forJustGcEd = shouldBeGced(gcNow, now);
Expand All @@ -31,7 +33,7 @@ void main() {
expect(forNotEnoughGc, isFalse);

final forEnoughTimeAndGc = shouldBeGced(
gcNow - gcCountBuffer,
gcNow - defaultGcCountBuffer,
now.subtract(disposalTimeBuffer),
);
expect(forEnoughTimeAndGc, isTrue);
Expand Down
Loading