Skip to content

Commit

Permalink
Reduce memory allocated for the renderCache (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
xvrh authored Nov 25, 2023
1 parent faf7d74 commit c7066ca
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 3.0.0-alpha.3
- Reduce the max memory used when using `enableRenderCache` (now limited to 50MB)
- Allow to configure the memory with a global settings:
```dart
Lottie.renderCacheMaxMemory = 75000000;
```

## 3.0.0-alpha.2
- Implement auto-orient
- Add support for layer blend mode
Expand Down
6 changes: 3 additions & 3 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.0.0-alpha.2"
version: "3.0.0-alpha.3"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -254,10 +254,10 @@ packages:
dependency: transitive
description:
name: plugin_platform_interface
sha256: da3fdfeccc4d4ff2da8f8c556704c08f912542c5fb3cf2233ed75372384a034d
sha256: f4f88d4a900933e7267e2b353594774fc0d07fb072b47eedcd5b54e1ea3269f8
url: "https://pub.dev"
source: hosted
version: "2.1.6"
version: "2.1.7"
pointycastle:
dependency: transitive
description:
Expand Down
12 changes: 11 additions & 1 deletion lib/src/lottie.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'composition.dart';
import 'l.dart';
import 'lottie_builder.dart';
import 'providers/lottie_provider.dart';
import 'render_cache.dart';

/// A widget to display a loaded [LottieComposition].
/// The [controller] property allows to specify a custom AnimationController that
Expand All @@ -15,6 +16,15 @@ class Lottie extends StatefulWidget {
/// The cache instance for recently loaded Lottie compositions.
static LottieCache get cache => sharedLottieCache;

/// The maximum memory to use when using `enableRenderCache`.
/// When the limit is reached, new frames are not put in the cache until some
/// memory is released. When an animation disappear from the screen, its memory
/// is released immediately.
static int get renderCacheMaxMemory => globalRenderCache.maxMemory;
static set renderCacheMaxMemory(int value) {
globalRenderCache.maxMemory = value;
}

const Lottie({
super.key,
required this.composition,
Expand Down Expand Up @@ -354,7 +364,7 @@ class Lottie extends StatefulWidget {
/// visible on the screen (with all transforms applied).
///
/// In order to not exceed the memory limit of a device, the cache is constrained
/// to maximum 100MiB. After that, animations are not cached anymore.
/// to maximum 50MB. After that, animations are not cached anymore.
final bool enableRenderCache;

static bool get traceEnabled => L.traceEnabled;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lottie_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ class LottieBuilder extends StatefulWidget {
/// visible on the screen (with all transforms applied).
///
/// In order to not exceed the memory limit of a device, the cache is constrained
/// to maximum 100MiB. After that, animations are not cached anymore.
/// to maximum 50MB. After that, animations are not cached anymore.
final bool? enableRenderCache;

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/src/raw_lottie.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class RawLottie extends LeafRenderObjectWidget {
/// visible on the screen (with all transforms applied).
///
/// In order to not exceed the memory limit of a device, the cache is constrained
/// to maximum 100MiB. After that, animations are not cached anymore.
/// to maximum 50MB. After that, animations are not cached anymore.
final bool enableRenderCache;

final FilterQuality? filterQuality;
Expand Down
6 changes: 4 additions & 2 deletions lib/src/render_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class RenderCache {
final entries = <CacheKey, RenderCacheEntry>{};
final handles = <Object, RenderCacheHandle>{};

/// The maximum memory this cache will use (default 100MiB)
/// The maximum memory this cache will use (default 50MB)
/// It should refuse to create new images if the size is exceeded.
final int maxMemory = 100 << 20;
static const int defaultMaxMemory = 50000000;

int maxMemory = defaultMaxMemory;

bool enableDebugBackground = false;

Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ packages:
dependency: "direct dev"
description:
name: dart_style
sha256: abd7625e16f51f554ea244d090292945ec4d4be7bfbaf2ec8cccea568919d334
sha256: "40ae61a5d43feea6d24bd22c0537a6629db858963b99b4bc1c3db80676f32368"
url: "https://pub.dev"
source: hosted
version: "2.3.3"
version: "2.3.4"
fake_async:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: lottie
description: Render After Effects animations natively on Flutter. This package is a pure Dart implementation of a Lottie player.
version: 3.0.0-alpha.2
version: 3.0.0-alpha.3
repository: https://github.com/xvrh/lottie-flutter

environment:
Expand Down

0 comments on commit c7066ca

Please sign in to comment.