redux-toolkit icon indicating copy to clipboard operation
redux-toolkit copied to clipboard

Memory leak after upgrading from 1.9.5 to 2.4.0

Open noveogroup-amorgunov opened this issue 8 months ago β€’ 6 comments

Hey. We encountered a memory leak problem on server (nextjs page router) after update redux-toolkit from 1.9.5 to 2.4.0. After investigating the heap snapshot, we found that the problem was in the reselect (describe in https://github.com/reduxjs/reselect/issues/635).

A temporary fix is ​​to use this lruMemoize for createSelector instead of weakMapMemoize. But anyway I think this information should be described in the documentation (may be in migration guide) because this is clearly not expected behavior (I mean memory leak).

diagram after fallback to lruMemoize in 02/20: Image

noveogroup-amorgunov avatar Mar 03 '25 13:03 noveogroup-amorgunov

Can you give an example of the overall usage patterns that you have in the app? Store creation, frequency of actions, what you're selecting?

markerikson avatar Mar 03 '25 17:03 markerikson

We would still very much like a reproduction of the situation you are seeing, but specific to a combination of SSR, default states, and selectors with primitive arguments, this repro might help to investigate: https://codesandbox.io/p/devbox/selectors-default-value-leak-c7g9rr

Core of that reproduction is:

  const slice = createSlice({
    name: "slice",
    initialState: {
      items: [],
    },
    reducers: {},
  });

  const selector1 = createSelector(
    [(fullState) => fullState.slice, (_, userId) => userId],
    (userState, userId) => ({ userId }),
    {
      memoize: weakMapMemoize,
      argsMemoize: weakMapMemoize,
    }
  );

  for (let i = 1; i <= 500; i++) {
    const store = configureStore({ reducer: { slice: slice.reducer } });
    selector1(store.getState(), i);
  }

The problem here being that the initial state of slice, { items: [] } never leaves memory and will be returned for every store created, even if then different primitive selector arguments are used beyond that.

Image

For debugging, I ran this locally and added this line to reselect.mjs

function weakMapMemoize(func, options = {}) {
  let fnNode = createCacheNode();
+  (globalThis.rootNodes ??= []).push(fnNode)
  const { resultEqualityCheck } = options;

Then at the end of the IIFE

  console.log('finished', globalThis.rootNodes)
  debugger;

Image

phryneas avatar Mar 03 '25 21:03 phryneas

Thanks for feedback, I will try to debug our code and provide more data

noveogroup-amorgunov avatar Mar 04 '25 17:03 noveogroup-amorgunov

Just thought I would mention, the patterns you were seeing for memory usage were exactly the same as we saw when upgrading 1.9.5->1.9.6 about a year ago. We were/are also using RTKQ in pages router along with next-redux-wrapper and the recommended patterns in getServerSideProps as the docs prescribe (store.dispatch(myApi.endpoints.myEndpoint.initiate(myParams)), then await Promise.all(store.dispatch(getRunningQueriesThunk())). We rolled back and still haven't upgraded beyond 1.9.5.

This issue could be related: https://github.com/reduxjs/redux-toolkit/issues/3988

harry-gocity avatar May 05 '25 15:05 harry-gocity

FWIW I'm still aware this issue is out there, but really need a good realistic repro project that shows how people are ending up in this situation.

markerikson avatar May 05 '25 16:05 markerikson

~~@markerikson thank you - I have some time this week & will try get you a reproduction.~~

~~(I appreciate until I actually give you a repro, I am just another person saying I will get you a repro πŸ˜…)~~

Created a repro but posted it over here as it's more relevant to just 1.9.5->1.9.6 https://github.com/reduxjs/redux-toolkit/issues/3988#issuecomment-2853614206

harry-gocity avatar May 05 '25 17:05 harry-gocity