question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Lottie causes memory leaks when navigating with react-router

See original GitHub issue

Tell us about your environment React SPA, using React-router for navigation.

Using lottie-web with a free animation from lottiefiles.com.

  • Browser and Browser Version: Chrome for Windows v80.0.3987.149 (Official Build) (64-bit)

  • After Effects Version: N/A

What did you do? Please explain the steps you took before you encountered the problem.

  1. Go to page with animation
  2. Navigate away from the page with a Link from react-router-dom
  3. Navigate back to the animation-page, again through react-router

What did you expect to happen? I expected that the animation would play as normal with no issues.

What actually happened? Please include as much relevant detail as possible. Memory leaks occurs. I made som Heap-snaps with the following results:

Heap-snap 1: 34MB Heap-snap 2: 70 MB Heap-snap 3: 410 MB

Tried to look at the loaded data from the json file by using `JSON.stringify(animationJson) to see if it’s content changed (doesn’t make sense that it should, right?)

When I check the size of the json-data WITHOUT loading it with lottie, the data-size is constant at 37.2 KB. When I load it with lottie it grows to 2.3 MB on second render, 36 MB on third render and 174 MB on fourth render.

Seems like lottie changes the cached json data for each rerender of the component. I tried stop/destroy for the specific animation and for all through the lottie-object.

My code:

...
import lottie from 'lottie-web';
import theAnimation from './assets/the_animation.json';

...
const AnimationComponent = () => {

// Other code
  
  const animElement = useRef();

  useEffect(() => {
    console.log(JSON.stringify(theAnimation));

    console.log('fireworks mount');
    const anim = lottie.loadAnimation({
      animationData: theAnimation,
      loop: numberOfIterations,
      renderer: 'svg',
      container: animElement.current,
      autoplay: true,
    });

    console.log(JSON.stringify(theAnimation));

    return () => {
      anim.stop();
      anim.destroy();
      lottie.stop();
      lottie.destroy();
    }
  }, [])
}

Please provide a download link to the After Effects file that demonstrates the problem. I don’t have the AE-file, but this is the animation I used

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

13reactions
bodymovincommented, Mar 31, 2020

Hi, this animation uses repeaters, which add up every time you use the same animation instance. I’d suggest that you import the data as a string and every time you load it, pass JSON.parse(animationString) as the animationData value.

2reactions
Kegulfcommented, Mar 31, 2020

Holy french pancake! (creppés)

It worked. You my good sir or ma’am are a HERO ❤️ Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lottie Animation in React causes Memory leak / browser ...
I'm using Lottie to display an animation on a page in a basic Create React App with React Router. When I navigate through...
Read more >
Memory Leaks, How to avoid them in a React App.
Memory leaks in React applications are primarily a result of not cancelling subscriptions made when a component was mounted before the component ...
Read more >
React Native Memory Leakage Animation Issue-Reactjs
[Solved]-React Native Memory Leakage Animation Issue-Reactjs ... You've got a lot of temporary variables, both simple numbers and objects. I'm not entirely sure ......
Read more >
how to resolve memory leak problem after using useLocation ...
import {useLocation} from 'react-router-dom'; export function useMutationHooks( variable: QueryJSON ): UseMutationResult<void, unknown, ...
Read more >
Animations tutorial - Flutter documentation
The most common behavior of a Listener is to call setState() to cause a rebuild. ... controller when the State object is discarded...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found