CJY0208 / react-activation

Hack <KeepAlive /> for React
https://www.npmjs.com/package/react-activation
MIT License
1.78k stars 140 forks source link

Memory leak because of memoizedFunction #307

Open faridsaid20 opened 6 months ago

faridsaid20 commented 6 months ago

Hi,

I have noticed a memory leak because of caching of element types in tryFixCtx function in fixContext.js file. As I see, tryFixCtx caches in a Map all the types that it gets. It continues forever even if the element of that type is unmounted long time ago. This often leads to detached references to nodes and thus memory leak. In our project it leads to 600 nodes added on each visit to a page. We don't event need to use keepAlive, on that page.

I already prepared a solution which you could consider. But sadly I cannot create Pull Request here, so sharing screenshots instead.

Step 1. Add weakMemoize funciton which is 99% copy of memoize, but using WeakMap instead. Notice added try catch, it was needed because WeakMap cannot have symbols as their keys.

image

Step 2. Use weakMemoize function instead of memoize

image