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

Created on 31 Mar 2020  路  5Comments  路  Source: airbnb/lottie-web

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

Most helpful comment

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.

All 5 comments

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.

Holy french pancake! (crepp茅s)

It worked. You my good sir or ma'am are a HERO <3 Thank you!

How did you see that it uses repeaters btw? For later referances, and for others that might encounter the same problem

I did a quick search on the json of the string "rp"

Thank you again! :blush:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

leantide picture leantide  路  3Comments

Ipaulsen picture Ipaulsen  路  4Comments

RenanSgorlom picture RenanSgorlom  路  3Comments

zhengs picture zhengs  路  3Comments

samiam2017 picture samiam2017  路  3Comments