Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out memory leaks in react native app?

I've build a learning management system app in react native android.I'm using AsyncStorage for simple state management and not used redux at all.The issue I'm facing now is if I'm going to use the app continuously by performing different actions then the app become very slow.I think it is memory leak because when I kill the app from background and open it again it is working without any delay.So I don't know how to avoid this memory leak.I've tried many solutions

  1. Removed all console.log from app
  2. Changed all inline styles
  3. Used ComponentDidMount instead of ComponentWillMount.
  4. Tried prefetching of data.

But I don't know how remove data from heap memory.Is the data is getting stored within heapon each navigation?So this will makes the app very slow in performance. I don't know whether I'm right.Excuse me if there is any mistake in my concept.There is no time to change the state management to redux now.Anybody please help me to find a solution , it will be a great help.Thank you!

like image 527
Linu Sherin Avatar asked Mar 19 '19 11:03

Linu Sherin


People also ask

How do I know if my app has a memory leak?

If you suspect you are running into a temporal leak, a good way to check is to use Android Studio's memory profiler. Once you start a session within the profiler, take the steps to reproduce the leak, but wait for a longer period of time before dumping the heap and inspecting. The leak may be gone after the extra time.

How do you identify memory leaks?

To find a memory leak, look at how much RAM the system is using. The Resource Monitor in Windows can be used to accomplish this. In Windows 8.1 and Windows 10: To open the Run dialogue, press Windows+R, then type "resmon" and click OK.

What causes memory leak in react native?

The memory leak will happen if the API server or host took some time to respond and the component was unmounted before the response was received. Though the component was unmounted, the response to the request will still be received on completion. The response will then be parsed and setData will be called.


2 Answers

I had the same issue, few methods that helped were:

Using transform-remove-console:

https://www.npmjs.com/package/babel-plugin-transform-remove-console

add this to your babel production plugins and install it. It will hide all the console logs in the app in production.

Adding a mounted state

Specifically, calling setState() in an unmounted component means that your app is still holding a reference to the component after the component has been unmounted - which often indicates a memory leak!

https://reactjs.org/blog/2015/12/16/ismounted-antipattern.html

import React, { Component } from 'react';


class App extends Component {

  _isMounted = false;

  componentDidMount() {
    this._isMounted = true;
    // ... do your stuff here
  }

  componentWillUnmount() {
    // tells the component that component is now unmounted
    this._isMounted = false;
  }

  getUsersFromApi(){
    if(this._isMounted){
      // ... tasks
    }
  }

}

export default App;
like image 135
Daniel Danaee Avatar answered Nov 05 '22 02:11

Daniel Danaee


I too had the same issue, because of calling setState on an unmounted component,

So, I usually have this template for any class-based component that has a state:

I forgot about setState(), and use setComponentState declared down:

class MyComponent extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      // other fields...

    };
    this.isUnmounted =  true,
  }

  componentDidMount(){
      this.isUnmounted =  false;
  }

  componentWillUnmount() {
      this.isUnmounted = true;
  }

  setComponentState = (values) => {
    if (!this.isUnmounted) this.setState(values);
  };
}
like image 6
Hend El-Sahli Avatar answered Nov 05 '22 02:11

Hend El-Sahli