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

Rename gcCountBuffer to numberOfGcCycles. #101

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions pkgs/leak_tracker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 9.0.0

* Rename `gcCountBuffer` to `numberOfGcCycles`.
polina-c marked this conversation as resolved.
Show resolved Hide resolved

# 8.0.3

* Fix an issue with custom gcCountBuffer values.
Expand Down
8 changes: 4 additions & 4 deletions pkgs/leak_tracker/lib/src/leak_tracking/_gc_counter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ bool shouldObjectBeGced({
required DateTime timeAtDisposal,
required int currentGcCount,
required DateTime currentTime,
required Duration disposalTimeBuffer,
required int gcCountBuffer,
required Duration disposalTime,
required int numberOfGcCycles,
}) =>
currentGcCount - gcCountAtDisposal >= gcCountBuffer &&
currentTime.difference(timeAtDisposal) >= disposalTimeBuffer;
currentGcCount - gcCountAtDisposal >= numberOfGcCycles &&
currentTime.difference(timeAtDisposal) >= disposalTime;
14 changes: 7 additions & 7 deletions pkgs/leak_tracker/lib/src/leak_tracking/_object_record.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,33 @@ class ObjectRecord {
bool get isGCed => _gcedGcCount != null;
bool get isDisposed => _disposalGcCount != null;

bool isGCedLateLeak(Duration disposalTimeBuffer, int gcCountBuffer) {
bool isGCedLateLeak(Duration disposalTime, int numberOfGcCycles) {
if (_disposalGcCount == null || _gcedGcCount == null) return false;
assert(_gcedTime != null);
return shouldObjectBeGced(
gcCountAtDisposal: _disposalGcCount!,
timeAtDisposal: _disposalTime!,
currentGcCount: _gcedGcCount!,
currentTime: _gcedTime!,
disposalTimeBuffer: disposalTimeBuffer,
gcCountBuffer: gcCountBuffer,
disposalTime: disposalTime,
numberOfGcCycles: numberOfGcCycles,
);
}

bool isNotGCedLeak(
int currentGcCount,
DateTime currentTime,
Duration disposalTimeBuffer,
int gcCountBuffer,
Duration disposalTime,
int numberOfGcCycles,
) {
if (_gcedGcCount != null) return false;
return shouldObjectBeGced(
gcCountAtDisposal: _disposalGcCount!,
timeAtDisposal: _disposalTime!,
currentGcCount: currentGcCount,
currentTime: currentTime,
disposalTimeBuffer: disposalTimeBuffer,
gcCountBuffer: gcCountBuffer,
disposalTime: disposalTime,
numberOfGcCycles: numberOfGcCycles,
);
}

Expand Down
14 changes: 7 additions & 7 deletions pkgs/leak_tracker/lib/src/leak_tracking/_object_tracker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class ObjectTracker implements LeakProvider {
/// The optional parameters are injected for testing purposes.
ObjectTracker({
this.leakDiagnosticConfig = const LeakDiagnosticConfig(),
required this.disposalTimeBuffer,
required this.gcCountBuffer,
required this.disposalTime,
required this.numberOfGcCycles,
FinalizerBuilder? finalizerBuilder,
GcCounter? gcCounter,
IdentityHashCoder? coder,
Expand All @@ -40,7 +40,7 @@ class ObjectTracker implements LeakProvider {
late IdentityHashCoder _coder;

/// Time to allow the disposal invoker to release the reference to the object.
final Duration disposalTimeBuffer;
final Duration disposalTime;

late FinalizerWrapper _finalizer;

Expand All @@ -52,7 +52,7 @@ class ObjectTracker implements LeakProvider {

final LeakDiagnosticConfig leakDiagnosticConfig;

final int gcCountBuffer;
final int numberOfGcCycles;

void startTracking(
Object object, {
Expand Down Expand Up @@ -93,7 +93,7 @@ class ObjectTracker implements LeakProvider {
final record = _notGCed(code);
record.setGCed(_gcCounter.gcCount, clock.now());

if (record.isGCedLateLeak(disposalTimeBuffer, gcCountBuffer)) {
if (record.isGCedLateLeak(disposalTime, numberOfGcCycles)) {
_objects.gcedLateLeaks.add(record);
} else if (record.isNotDisposedLeak) {
_objects.gcedNotDisposedLeaks.add(record);
Expand Down Expand Up @@ -181,8 +181,8 @@ class ObjectTracker implements LeakProvider {
if (_notGCed(code).isNotGCedLeak(
_gcCounter.gcCount,
now,
disposalTimeBuffer,
gcCountBuffer,
disposalTime,
numberOfGcCycles,
)) {
_objects.notGCedDisposedOk.remove(code);
_objects.notGCedDisposedLate.add(code);
Expand Down
4 changes: 2 additions & 2 deletions pkgs/leak_tracker/lib/src/leak_tracking/leak_tracker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void enableLeakTracking({

final newTracker = ObjectTracker(
leakDiagnosticConfig: theConfig.leakDiagnosticConfig,
disposalTimeBuffer: theConfig.disposalTimeBuffer,
gcCountBuffer: theConfig.gcCountBuffer,
disposalTime: theConfig.disposalTime,
numberOfGcCycles: theConfig.numberOfGcCycles,
);

_objectTracker.value = newTracker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ class LeakDiagnosticConfig {
///
/// Theoretically, 2 should be enough, however it gives false positives
/// if there is no activity in the application for ~5 minutes.
const defaultGcCountBuffer = 3;
const defaultNumberOfGcCycles = 3;

class LeakTrackingConfiguration {
const LeakTrackingConfiguration({
this.stdoutLeaks = true,
this.notifyDevTools = true,
this.onLeaks,
this.checkPeriod = const Duration(seconds: 1),
this.disposalTimeBuffer = const Duration(milliseconds: 100),
this.disposalTime = const Duration(milliseconds: 100),
this.leakDiagnosticConfig = const LeakDiagnosticConfig(),
this.gcCountBuffer = defaultGcCountBuffer,
this.numberOfGcCycles = defaultNumberOfGcCycles,
});

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

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

final LeakDiagnosticConfig leakDiagnosticConfig;

Expand All @@ -135,7 +135,7 @@ class LeakTrackingConfiguration {
final LeakSummaryCallback? onLeaks;

/// Time to allow the disposal invoker to release the reference to the object.
final Duration disposalTimeBuffer;
final Duration disposalTime;
}

/// Configuration for leak tracking in unit tests.
Expand Down
16 changes: 8 additions & 8 deletions pkgs/leak_tracker/lib/src/leak_tracking/orchestration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ 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, that should be enough for
/// [numberOfGcCycles] is number of full GC cycles, that should be enough for
/// a non reachable object to be GCed.
/// If after this number of GC cycles a disposed object is still not garbage collected,
/// it is considered a notGCed leak.
/// Theoretically, the value 1 should be enough, but in practice it creates false
/// positives for stale applications.
/// So, recommended value for applications is 3, and for tests is 1.
/// So, recommended value for applications is 3, and for tests is 2.
///
/// If you test Flutter widgets, connect their instrumentation to the leak
/// tracker:
Expand Down Expand Up @@ -87,12 +87,12 @@ Future<Leaks> withLeakTracking(
Duration? timeoutForFinalGarbageCollection,
LeakDiagnosticConfig leakDiagnosticConfig = const LeakDiagnosticConfig(),
AsyncCodeRunner? asyncCodeRunner,
int gcCountBuffer = defaultGcCountBuffer,
int numberOfGcCycles = defaultNumberOfGcCycles,
}) async {
if (gcCountBuffer <= 0) {
if (numberOfGcCycles <= 0) {
throw ArgumentError.value(
gcCountBuffer,
'gcCountBuffer',
numberOfGcCycles,
'numberOfGcCycles',
'Must be positive.',
);
}
Expand All @@ -103,7 +103,7 @@ Future<Leaks> withLeakTracking(
resetIfAlreadyEnabled: true,
config: LeakTrackingConfiguration.passive(
leakDiagnosticConfig: leakDiagnosticConfig,
gcCountBuffer: gcCountBuffer,
numberOfGcCycles: numberOfGcCycles,
),
);

Expand All @@ -123,7 +123,7 @@ Future<Leaks> withLeakTracking(
}

await forceGC(
fullGcCycles: gcCountBuffer,
fullGcCycles: numberOfGcCycles,
timeout: timeoutForFinalGarbageCollection,
);
leaks = await collectLeaks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ void main() {

tearDown(() => disableLeakTracking());

for (var gcCountBuffer in [1, defaultGcCountBuffer]) {
test('Leak tracker respects maxRequestsForRetainingPath, $gcCountBuffer.',
for (var numberOfGcCycles in [1, defaultNumberOfGcCycles]) {
test(
'Leak tracker respects maxRequestsForRetainingPath, $numberOfGcCycles.',
() async {
LeakTrackerGlobalSettings.maxRequestsForRetainingPath = 2;
final leaks = await withLeakTracking(
Expand All @@ -30,7 +31,7 @@ void main() {
leakDiagnosticConfig: const LeakDiagnosticConfig(
collectRetainingPathForNonGCed: true,
),
gcCountBuffer: gcCountBuffer,
numberOfGcCycles: numberOfGcCycles,
);

const pathHeader = ' path: >';
Expand All @@ -52,7 +53,7 @@ void main() {
);
});

test('Retaining path for not GCed object is reported, $gcCountBuffer.',
test('Retaining path for not GCed object is reported, $numberOfGcCycles.',
() async {
final leaks = await withLeakTracking(
() async {
Expand All @@ -62,7 +63,7 @@ void main() {
leakDiagnosticConfig: const LeakDiagnosticConfig(
collectRetainingPathForNonGCed: true,
),
gcCountBuffer: gcCountBuffer,
numberOfGcCycles: numberOfGcCycles,
);

const expectedRetainingPathTails = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ void main() {
test('shouldObjectBeGced', () {
final now = DateTime(2022);
const gcNow = 1000;
const disposalTimeBuffer = Duration(milliseconds: 100);
const timeToDispose = Duration(milliseconds: 100);

bool shouldBeGced(int disposalGcCount, DateTime disposalTime) =>
shouldObjectBeGced(
gcCountAtDisposal: disposalGcCount,
timeAtDisposal: disposalTime,
currentGcCount: gcNow,
currentTime: now,
disposalTimeBuffer: disposalTimeBuffer,
gcCountBuffer: defaultGcCountBuffer,
disposalTime: timeToDispose,
numberOfGcCycles: defaultNumberOfGcCycles,
);

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

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