Memory leak when render view
Hello, when I set this.setState({lotsOfData: data}) for rendering view, and then I set this.setState({lotsOfData: []}) for remove view, it will cause memory leak problem, but view component is unmount.
Step 1. render 1500 text view Step 2. set data to [] for unmounting view.
P.S. The figure 3 is setState without render text view.
figure1
figure2
figure3
@scott108 I would expect if you ran steps 1 and 2 multiple times you'd see something looking like a step function of the memory leak over time. Can you try to produce this? From the current screenshots, its not clear if the issue is actually a memory leak or if you're just hitting additional JavaScript code that gets JIT'd, thus increasing the overall memory use.
Hello @rozele , here is my code, I set state after 8 seconds, and you can see memory is growing up. After 7 seconds, it will set movies to empty array, and the memory is growing down, but it's not the initial memory usage.

import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
class memorytest extends Component {
constructor(props) {
super(props);
this.state = {
movies: []
}
this.clearMovies = this.clearMovies.bind(this);
this.setMovies = this.setMovies.bind(this);
}
async getMoviesFromApi() {
try {
let response = await fetch('https://facebook.github.io/react-native/movies.json');
let responseJson = await response.json();
return responseJson.movies;
} catch(error) {
console.error(error);
}
}
clearMovies() {
this.setState({movies: []});
}
setMovies() {
this.setState({movies:this.state.movies});
}
async componentWillMount() {
let movies = await this.getMoviesFromApi();
for(let i = 0; i < 300; i++) {
this.state.movies = this.state.movies.concat(movies);
}
//console.log(this.state.movies);
setTimeout(() => {this.setMovies()}, 8000);
setTimeout(() => {this.clearMovies()}, 15000);
}
_renderRow() {
return this.state.movies.map((movie, index) => {
return (
<Text key={index}>{movie.title}</Text>
)
})
}
render() {
return (
<View style={styles.container}>
{this._renderRow()}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
});
@scott108, the increased memory is not indicative of a memory leak. I changed your example to render movies and clear them every 10 seconds and the heap size / process memory does not increase over time. The memory increase you see from the first movie rendering is due to memory usage in the framework. There are various data structures (dictionaries, lazy delegates, etc.) that contribute to the extra memory but are not a leak per se. There is also memory used by the JavaScript runtime for JIT'd code.