Potential memory leak with arrays in shared values
See original GitHub issueDescription
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.
Shareable value pointers should be cleared up when re-assigning the value but instead we are seeing 1131065 shared value pointer references:
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:
- Created 2 years ago
- Reactions:9
- Comments:17 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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?