Clear asset manager caches on memory pressure (#91353)
This commit is contained in:
parent
22c1ca769b
commit
5112f86c94
@ -98,6 +98,9 @@ abstract class AssetBundle {
|
||||
/// loaded, the cache will be reread from the asset bundle.
|
||||
void evict(String key) { }
|
||||
|
||||
/// If this is a caching asset bundle, clear all cached data.
|
||||
void clear() { }
|
||||
|
||||
@override
|
||||
String toString() => '${describeIdentity(this)}()';
|
||||
}
|
||||
@ -215,6 +218,12 @@ abstract class CachingAssetBundle extends AssetBundle {
|
||||
_stringCache.remove(key);
|
||||
_structuredDataCache.remove(key);
|
||||
}
|
||||
|
||||
@override
|
||||
void clear() {
|
||||
_stringCache.clear();
|
||||
_structuredDataCache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// An [AssetBundle] that loads resources using platform messages.
|
||||
|
@ -113,7 +113,9 @@ mixin ServicesBinding on BindingBase, SchedulerBinding {
|
||||
/// [SystemChannels.system].
|
||||
@protected
|
||||
@mustCallSuper
|
||||
void handleMemoryPressure() { }
|
||||
void handleMemoryPressure() {
|
||||
rootBundle.clear();
|
||||
}
|
||||
|
||||
/// Handler called for messages received on the [SystemChannels.system]
|
||||
/// message channel.
|
||||
|
@ -51,8 +51,10 @@ class TestBinding extends BindingBase with SchedulerBinding, ServicesBinding {
|
||||
}
|
||||
|
||||
void main() {
|
||||
final TestBinding binding = TestBinding();
|
||||
|
||||
test('Adds rootBundle LICENSES to LicenseRegistry', () async {
|
||||
TestBinding().defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData? message) async {
|
||||
binding.defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData? message) async {
|
||||
if (const StringCodec().decodeMessage(message) == 'NOTICES.Z' && !kIsWeb) {
|
||||
return Uint8List.fromList(gzip.encode(utf8.encode(combinedLicenses))).buffer.asByteData();
|
||||
}
|
||||
@ -76,4 +78,33 @@ void main() {
|
||||
equals(<String>['L2Paragraph1', 'L2Paragraph2', 'L2Paragraph3']),
|
||||
);
|
||||
});
|
||||
|
||||
test('didHaveMemoryPressure clears asset caches', () async {
|
||||
int flutterAssetsCallCount = 0;
|
||||
binding.defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData? message) async {
|
||||
flutterAssetsCallCount += 1;
|
||||
return Uint8List.fromList('test_asset_data'.codeUnits).buffer.asByteData();
|
||||
});
|
||||
|
||||
await rootBundle.loadString('test_asset');
|
||||
expect(flutterAssetsCallCount, 1);
|
||||
await rootBundle.loadString('test_asset2');
|
||||
expect(flutterAssetsCallCount, 2);
|
||||
await rootBundle.loadString('test_asset');
|
||||
expect(flutterAssetsCallCount, 2);
|
||||
await rootBundle.loadString('test_asset2');
|
||||
expect(flutterAssetsCallCount, 2);
|
||||
|
||||
final ByteData message = const JSONMessageCodec().encodeMessage(<String, dynamic>{'type': 'memoryPressure'})!;
|
||||
await ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('flutter/system', message, (_) { });
|
||||
|
||||
await rootBundle.loadString('test_asset');
|
||||
expect(flutterAssetsCallCount, 3);
|
||||
await rootBundle.loadString('test_asset2');
|
||||
expect(flutterAssetsCallCount, 4);
|
||||
await rootBundle.loadString('test_asset');
|
||||
expect(flutterAssetsCallCount, 4);
|
||||
await rootBundle.loadString('test_asset2');
|
||||
expect(flutterAssetsCallCount, 4);
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user