react-native-windows icon indicating copy to clipboard operation
react-native-windows copied to clipboard

Memory leak when render view

Open scott108 opened this issue 9 years ago • 4 comments

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.

scott108 avatar Jul 01 '17 08:07 scott108

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.

1 figure1 2 figure2 3 figure3

scott108 avatar Jul 03 '17 01:07 scott108

@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.

rozele avatar Jul 03 '17 16:07 rozele

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.

test

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 avatar Jul 04 '17 01:07 scott108

@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.

rozele avatar Jul 04 '17 13:07 rozele