Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How in React native destroy an component instance to avoid memory leak and release underlie views objects of native code?

Tags:

I want to destroy some component that i have instantiated to release memory. In my current app almost every view that i instantiate and then release it (remove reference to it) doesn't get garbage collected. I keep no reference to to views. I'm not sure if this memory leak is caused by my app or it's react-native(and react native have some memory leaks problems). is there a way to confidently destroy a view instance ?

like image 406
Hamid Karimi Avatar asked Dec 23 '15 03:12

Hamid Karimi


People also ask

How does react native prevent memory leaks?

Memory Leaks in React NativePerf Monitor is a good choice to address Android native memory leak. Import PerfMonitor from 'react-native/Libraries/Performance/RCTRenderingPerf'; PerfMonitor. toggle(); PerfMonitor.

What causes memory leak in react native?

React components that perform state updates and run asynchronous operations can cause memory leak issues if the state is updated after the component is unmounted.

How do you destroy components in React?

React has a top-level API called unmountComponentAtNode() that removes a component from a specific container. The function unmountComponentAtNode() takes an argument as a container from which the specific component should be removed.

How do I clear memory in react native?

Open Android Studio then goes to menu Tools -> AVD Manager. Then select the menu in your selected simulator and click on wipe data.


1 Answers

React will destroy a component when you don't render it anymore. or when you omit it from virtual DOM.

const [render, setRender] = useState(true)  <View>    {render      ? <HeavyComponent/>          : null    }    <AnotherComponent/> </View> 
like image 74
TheEhsanSarshar Avatar answered Sep 21 '22 12:09

TheEhsanSarshar