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.

Potential memory leak with arrays in shared values

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:9
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
VinceAbruzzesecommented, Aug 19, 2022

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

4reactions
amsimoncommented, May 12, 2022

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Find a memory leak among arrays - Stack Overflow
My program is intended to take a 1 integer argument, create an array of that size, and then create ANOTHER array of double...
Read more >
Memory leak in C++ and How to avoid it? - GeeksforGeeks
The best way to avoid memory leaks in C++ is to have as few new/delete calls at the program level as possible –...
Read more >
How To Detect and Prevent Memory Leaks | Scout APM Blog
The above example is likely to cause a memory leak because the variable requests, which holds a new instance of the Map object,...
Read more >
Causes of Memory Leaks in JavaScript and How to Avoid Them
A memory leak occurs when an object in memory that is supposed to be cleaned in a garbage collection cycle stays reachable from...
Read more >
Memory leak - Wikipedia
In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in...
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