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
- Mount a list of Lottie components inside a modal-like component
- Unmount the component continuously
- 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
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
- fix: memory leak on iOS on deallocation Fixes: https://github.com/lottie-react-native/lottie-react-native/issues/1010 — committed to lottie-react-native/lottie-react-native by matinzd a year ago
- fix: memory leak on iOS on deallocation (#1055) * fix: memory leak on iOS on deallocation Fixes: https://github.com/lottie-react-native/lottie-react-native/issues/1010 * fix: add weak reference... — committed to lottie-react-native/lottie-react-native by matinzd a year ago
- fix: memory leak on iOS on deallocation (#1055) * fix: memory leak on iOS on deallocation Fixes: https://github.com/lottie-react-native/lottie-react-native/issues/1010 * fix: add weak reference... — committed to alyssaharvey3/react-native-lottie by alyssaharvey3 a year ago
- fix: memory leak on iOS on deallocation (#1055) * fix: memory leak on iOS on deallocation Fixes: https://github.com/lottie-react-native/lottie-react-native/issues/1010 * fix: add weak reference... — committed to GoldenDragon0710/Lottie-React-Native by GoldenDragon0710 a year ago
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
The fix is included in v6.0.0-rc.8:
https://github.com/lottie-react-native/lottie-react-native/releases/tag/v6.0.0-rc.8
@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? 😄
I put some time into that but still no luck. I will try again on the weekend.
Thanks for your quick reply.
In our organizational code, the new architecture is not turned on. I could see no changes by adding
removeClippedSubviewsthough