Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release Lottie File on unmount #676

Closed
NicholasNations opened this issue Aug 21, 2020 · 15 comments · Fixed by #1055
Closed

Release Lottie File on unmount #676

NicholasNations opened this issue Aug 21, 2020 · 15 comments · Fixed by #1055

Comments

@NicholasNations
Copy link

NicholasNations commented Aug 21, 2020

I am running quite a few Lottie Animations in my app and every time it runs, the animation never releases. Is there a way to clear the local memory after running the animation? At least the current animation. My memory get's up to 2gbs pretty fast. I am running animations all over the app and every time I unmount and come back, it adds the same animation into the memory. I start at 470 mbs, then I see an animation, i have 500 mbs, then I leave, it stays at 500 mbs, then I come back to the same animation page and it goes up to 530 mbs. Unmounting the component seems to make it stick in the internal memory. Any information as to why why the animation never releases and how to release it would be greatly appreciated.

Here is a component that is never released from memory:
<LottieView
style={{height: '100%', width: '100%'}}
source={source}
ref={animation}
enableMergePathsAndroidForKitKatAndAbove
autoPlay
/>

@NicholasNations NicholasNations changed the title Lottie Memory Leak. Release Lottie File on unmount Sep 1, 2020
@taneba
Copy link

taneba commented Sep 3, 2020

I was facing the similar issue, and in my case it was solve by running reset when component unmounts.
I created a helper hook that can start animation on mount and reset on unmount:

export function useLottieAnim() {
  const animation = useRef<LottieView>(null)

  useEffect(() => {
    if (animation.current) {
      animation.current.play()
    }
    return () => {
      animation.current && animation.current.reset()
    }
  }, [])

  return animation
}

// usage
const animation = useLottieAnim()

<LottieView
 source={source}
 ref={animation}
/>

@wanderSX
Copy link

wanderSX commented Sep 13, 2021

@NicholasNations Did the solution above help you, because it did not for me. Could it be because i have a dynamic source?

@MorenoMdz
Copy link

@NicholasNations Did the solution above help you, because it did not for me. Could it be because i have a dynamic source?

If you are using React Navigation this may help:

import { useRef, useCallback } from 'react'
import { useFocusEffect } from '@react-navigation/native'
import LottieView from 'lottie-react-native'

export function useLottieAnim() {
  const animation = useRef<LottieView>(null)

  useFocusEffect(
    useCallback(() => {
      if (animation.current) {
        animation.current.play()
      }
      return () => {
        animation.current && animation.current.reset()
      }
    }, []),
  )

  return animation
}

@jogoool
Copy link

jogoool commented Apr 4, 2022

same issue here, reset is not helping.

@stale
Copy link

stale bot commented Aug 14, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale There has been a lack of activity on this issue and it may be closed soon. label Aug 14, 2022
@stale
Copy link

stale bot commented Aug 30, 2022

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

@stale stale bot closed this as completed Aug 30, 2022
@nikitph
Copy link

nikitph commented Nov 18, 2022

This is a critical bug. The reset does help but this needs to be addressed natively

@longnc100500
Copy link

same issue

@keselme90
Copy link

keselme90 commented Feb 1, 2023

@longnc100500 @nikitph Did you guys find a solution?

@nikitph
Copy link

nikitph commented Feb 1, 2023 via email

@nikitph
Copy link

nikitph commented Feb 2, 2023

useFocusEffect( useCallback(() => { if (animation.current) { animation.current.play(); } return () => { animation.current && animation.current.reset(); }; }, []), );

@nikitph
Copy link

nikitph commented Feb 2, 2023

this will help u get back the ram after the animation finishes and u nav out
but the issue still needs to be addressed in terms of better ram use

@pSapien
Copy link

pSapien commented Apr 17, 2023

Not sure why this issue has been closed, it still persists. I have raised another issue for this
#1010

@matinzd matinzd reopened this May 14, 2023
@stale stale bot removed the stale There has been a lack of activity on this issue and it may be closed soon. label May 14, 2023
@matinzd
Copy link
Collaborator

matinzd commented May 17, 2023

Same here: #1010

@matinzd matinzd closed this as completed Jul 24, 2023
@matinzd
Copy link
Collaborator

matinzd commented Jul 24, 2023

Fixed by #1055.

@matinzd matinzd linked a pull request Jul 24, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants