Skip to content

Commit 9b5c780

Browse files
authored
Force GC before GC-sensitive benchmark runs (flutter#23289)
1 parent 7aeb539 commit 9b5c780

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

dev/benchmarks/complex_layout/test_driver/scroll_perf_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ void main() {
2626
// benchmark has greater noise.
2727
// See: https://github.com/flutter/flutter/issues/19434
2828
await Future<void>.delayed(const Duration(milliseconds: 250));
29+
30+
await driver.forceGC();
31+
2932
final Timeline timeline = await driver.traceAction(() async {
3033
// Find the scrollable stock list
3134
final SerializableFinder list = find.byValueKey(listKey);

dev/benchmarks/complex_layout/test_driver/semantics_perf_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ void main() {
2727
// Let app become fully idle.
2828
await Future<void>.delayed(const Duration(seconds: 2));
2929

30+
await driver.forceGC();
31+
3032
final Timeline timeline = await driver.traceAction(() async {
3133
expect(await driver.setSemantics(true), isTrue);
3234
});

packages/flutter_driver/lib/src/driver/driver.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class FlutterDriver {
137137
static const String _setVMTimelineFlagsMethodName = '_setVMTimelineFlags';
138138
static const String _getVMTimelineMethodName = '_getVMTimeline';
139139
static const String _clearVMTimelineMethodName = '_clearVMTimeline';
140+
static const String _collectAllGarbageMethodName = '_collectAllGarbage';
140141

141142
static int _nextDriverId = 0;
142143

@@ -791,6 +792,22 @@ class FlutterDriver {
791792
return result;
792793
}
793794

795+
/// Force a garbage collection run in the VM.
796+
Future<void> forceGC() async {
797+
try {
798+
await _peer
799+
.sendRequest(_collectAllGarbageMethodName, <String, String>{
800+
'isolateId': 'isolates/${_appIsolate.numberAsString}',
801+
});
802+
} catch (error, stackTrace) {
803+
throw DriverError(
804+
'Failed to force a GC due to remote error',
805+
error,
806+
stackTrace,
807+
);
808+
}
809+
}
810+
794811
/// Closes the underlying connection to the VM service.
795812
///
796813
/// Returns a [Future] that fires once the connection has been closed.

0 commit comments

Comments
 (0)