Skip to content

Memory leak after upgrading from 1.9.5 to 2.4.0 #4871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
noveogroup-amorgunov opened this issue Mar 3, 2025 · 6 comments
Open

Memory leak after upgrading from 1.9.5 to 2.4.0 #4871

noveogroup-amorgunov opened this issue Mar 3, 2025 · 6 comments

Comments

@noveogroup-amorgunov
Copy link

noveogroup-amorgunov commented Mar 3, 2025

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 reduxjs/reselect#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

@markerikson
Copy link
Collaborator

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?

@phryneas
Copy link
Member

phryneas commented Mar 3, 2025

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

@noveogroup-amorgunov
Copy link
Author

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

@harry-gocity
Copy link

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: #3988

@markerikson
Copy link
Collaborator

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.

@harry-gocity
Copy link

harry-gocity commented May 5, 2025

@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 #3988 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants