react-native-reanimated: Potential memory leak with arrays in shared values

Description

I’m noticing a spike in memory usage over time when updating a shared value in react-native-reanimated-2. I suspect that it may be an issue in C++ when deleting array pointer references.

Expected behavior

Memory usage should stay the same over time. I left this code running for 10 minutes and checked the memory usage through xcode and it was much higher.

Screen Shot 2022-01-11 at 4 14 12 PM

Shareable value pointers should be cleared up when re-assigning the value but instead we are seeing 1131065 shared value pointer references:

Screen Shot 2022-01-11 at 4 20 10 PM

Actual behavior & steps to reproduce

Create a react-native-reanimated-2 playground that reassigns a shared value to an object with an array and set an interval to continuously update it

Snack or minimal code example

import {useSharedValue} from 'react-native-reanimated';
import {View, Text} from 'react-native';
import React, {useEffect, useState} from 'react';

export default function AnimatedStyleUpdateExample(props) {
  const [obj, setObj] = useState();

  const randomObject = () => {
    const arr = [];
    for (let i = 0; i < 10000; i++) {
      arr.push({
        key: i,
        value: Math.random(),
      });
    }

    return {
      id: Math.random(),
      arr,
    };
  };

  const test = useSharedValue();

  useEffect(() => {
    const interval = setInterval(() => {
      setObj(randomObject());
    }, 500);

    return () => clearInterval(interval);
  }, []);

  useEffect(() => {
    test.value = obj;
  }, [obj, test]);

  return (
    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
      <Text>Hello World</Text>
      <Text>{test.value ? test.value.id : ''}</Text>
    </View>
  );
}

Package versions

name version
react-native 0.65.1
react-native-reanimated 2.3.1
NodeJS 17.2.0
Xcode 13.2.1
Java
Gradle
expo

Affected platforms

  • Android
  • iOS
  • Web

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 9
  • Comments: 18 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Any updates here? The library is pretty unusable in this state.

Seeing this issue on iOS. Using Reanimated with arrays is a pretty standard use case and makes it unusable for me.

What is the status on this?

We started investigating this issue and after some testing it seems like there is something wacky happening on the JS VM garbage collector side. Manually triggering garbage collector on both UI and RN JS VMs results in all the objects being cleared up. We’d normally expect the GC to kick in at some point and dealloc all those hanging objects, however it seems like that’s not the case and in our testing we cal also see that even after significant time we still don’t get these objects cleaned up property. We will continue this investigation and share more updates as we discover more details about this issue.

Can confirm we’re seeing important memory leaks on arrays in shared values as well

hey @VinceAbruzzese – the patch I posted hasn’t been merged to the library so pulling latest version won’t make the gc function available on the UI thread.

Then you need to trigger gc at some moment both on the main JS and UI threads, for example with this method:

import { runOnUI } from 'react-native-reanimated';

function triggerGC() {
  global.gc();
  runOnUI(() => {
    'worklet';
    global.gc();
  })();
}