Skip to content

The memory was not cleared in a timely manner #384

@MQTwist

Description

@MQTwist

Code

import 'package:flame_lottie/flame_lottie.dart';
import 'package:flutter/material.dart';
import 'package:flutter_application_1/page/widgets/ui/vendor_ui/manager/lottie_cache_manager.dart';
import 'package:get/get.dart';

class LottiePage extends StatefulWidget {
  const LottiePage({super.key});

  @override
  State<LottiePage> createState() => _LottiePageState();
}

class _LottiePageState extends State<LottiePage> {
  List<String> lotties = [
    "assets/lottie/home_top_1/data.json",
    "assets/lottie/home_top_2/data.json",
    "assets/lottie/home_top_3/data.json",
    "assets/lottie/home_top_banner_billon/data.json",
    "assets/lottie/home_top_banner_room/data.json",
    "assets/lottie/home_top_banner_stars/data.json",
  ];

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    Lottie.cache.clear();
    LottieCacheManager.instance.clear();
    super.dispose();
    debugPrint(">>>LottiePage dispose");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(Get.arguments),
        actions: [
          IconButton(
              onPressed: () {
                Get.toNamed("EmptyPage");
              },
              icon: const Icon(Icons.abc))
        ],
      ),
      body: _body(),
    );
  }

  Widget _body() {
    return SizedBox.expand(
      child: ListView.builder(
        itemBuilder: _item,
        itemCount: 100,
        itemExtent: 100,
      ),
    );
  }

  Widget _item(BuildContext context, int index) {
    String lottie = lotties[index % lotties.length];
    return Lottie.asset(lottie);
  }
}

I called the method of clearing memory when dispose

Lottie.cache.clear();
LottieCacheManager.instance.clear();

Below are screenshots of memory during push and pop operations

Before push

QQ_1733120811986

Push and show animation

QQ_1733120883519

Pop page and clear

QQ_1733120936501

Flutter doctor output

[✓] Flutter (Channel stable, 3.22.2, on macOS 13.4 22F66 darwin-x64, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[!] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    ! Flutter recommends a minimum Xcode version of 15.
      Download the latest version or update via the Mac App Store.
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.2)
[✓] VS Code (version 1.95.3)
[✓] Connected device (4 available)
[✓] Network resources

lottie version 3.1.3,Device is iPhone 8, system version 16.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions