I am using Flatlist and SectionList in my react native project and I have 300+ rows of data. However, I found a serious problem which is when I keep scrolling down and up, the memory usage is getting higher and higher. How can I solve this problem? Or how can I release the memory?
I know there are some related questions on here but I've tried many solutions and none of them works.
For examples,
1. I used Pure.component or shouldcomponentUpdate
2. I used some props of Flatlist and SectionList
initialNumToRender={9}
windowSize={10}
maxToRenderPerBatch={2}
removeClippedSubviews={true}
disableVirtualization={true}
getItemLayout={this.getItemLayout}
keyExtractor={(item, index) => item[0]}
extraData={this.state}
Is there any other solutions can help me solve the problems? Thanks a lot!
disableVirtualization={true}
basically turns off the virtualization features that FlatList offers, so I don't recommend it if memory is a concern. So I'd start by removing this prop.
Then, I'd investigate if the problem is the that there are too many items (so it takes too much RAM to keep them in the UI), or if there is a memory leak in your items (so even after they are removed from the UI they still consume memory)
FlatList's windowSize controls how many "invisible" items will be kept rendered. If you set windowSize to "1", only the visible items will be rendered (try that and see what happens when you scroll the FlatList). A windowSize of "21" (the default value) will render the visible items, plus 10 "windows" to the left and to the right (or the top and to the bottom) of the visible area. So, if the window measures, let's say 1000px, any items that are invisible right now but are less than 10000px from the visible area will be rendered by the FlatList ahead of time.
This is how I would approach the problem:
Memory leaks can be tough to find, so I'm hoping simply adjusting the windowSize and a few other settings will give the results you need. If there are memory leaks, this is an interesting article I've read recently: https://blog.swmansion.com/hunting-js-memory-leaks-in-react-native-apps-bd73807d0fde
Also, avoid checking for RAM usage using debug builds: not only they use more memory, debug facilities (like console.log and things like that) may create leaks that don't actually exist in release builds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With