lottie-react-native: memory leak while unmounting the lottie component

Description

The Lottie React Native component seems to be causing a memory leak when unmounted. After the component is unmounted, the device does not release the memory, which causes the RAM usage to spike up. Haven’t checked in Android devices yet.

Steps to Reproduce

  1. Mount a list of Lottie components inside a modal-like component
  2. Unmount the component continuously
  3. Watch the RAM level spike up
  • Code
import {useState, useRef, useEffect} from 'react';
import {View, Text, StatusBar} from 'react-native';
import LottieView from 'lottie-react-native';

StatusBar.setHidden(true);

const lottieImage = require('./assets/money_mouth_face.json');
const RE_RENDER_LIMIT = 50;
const INTERVAL_LIMIT = 1500;
const LOTTIE_VIEW_COUNT = 12;

export default function App() {
  const [show, setShow] = useState(false);
  const renderCountRef = useRef(0);

  useEffect(() => {
    const lastIntervalRef = setInterval(() => {
      if (renderCountRef.current >= RE_RENDER_LIMIT) {
        setShow(false);
        clearInterval(lastIntervalRef);
      } else {
        renderCountRef.current++;
        setShow(p => !p);
      }
    }, INTERVAL_LIMIT);
  }, []);

  if (renderCountRef.current >= RE_RENDER_LIMIT) return <Text>Done...</Text>;

  return (
    <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
      {show ? (
        Array.from({length: LOTTIE_VIEW_COUNT}).map((p, idx) => {
          return (
            <LottieView
              source={lottieImage}
              style={{height: 40, width: 40}}
              key={idx}
              autoPlay
              renderMode="HARDWARE"
            />
          );
        })
      ) : (
        <Text>Re-rendering...</Text>
      )}
    </View>
  );
}

Repo: https://github.com/pSapien/lottie-memory-leak

https://user-images.githubusercontent.com/26534891/232324421-0737fd99-9731-482d-b6b5-7be5c644b282.mov

Expected behavior:

  • When the Lottie React Native component is unmounted, I expect the device to release the memory used by the component, which should prevent the app’s RAM usage from spiking up.

Actual behavior:

When the Lottie React Native component is unmounted, the device does not release the memory used by the component, causing the memory leak.

Versions

lottie-react-native: 6.0.0-rc.3, 6.0.0-rc.2, 5.1.5

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 16

Commits related to this issue

Most upvoted comments

Now, we have addressed the issue and the pull request is in review.

Waiting for @emilioicai with higher access to approve.

It appears that Android is not experiencing this issue, as all views are being correctly managed and RAM is released as soon as the view is unmounted. However, I’ll investigate further to determine what might be going on behind the scenes with iOS.

https://github.com/lottie-react-native/lottie-react-native/assets/24797481/cbc2de7d-4e43-4e0f-812d-e327f6fec2a1

@matinzd Perhaps. I dont know how or why the above solution works. Just that we get back the RAM engaged by lottie animation this way which we otherwise dont. would love for a more elegant solution if one is possible. 🤘

@matinzd have u tried? 😄

Any news @matinzd ?

I put some time into that but still no luck. I will try again on the weekend.

Thank you for the detailed explanation. I will try to look into that on the weekend.

However, the reset method does not unmount the view. It just resets the frame. @nikitph

Do you have new architecture turned on? Meanwhile, can you try adding removeClippedSubviews on the outer view and see how it behaves? @pSapien

Thanks for your quick reply.

In our organizational code, the new architecture is not turned on. I could see no changes by adding removeClippedSubviews though