Skip to content
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

Memory leak after upgrading from 1.9.5 to 2.4.0 #4871

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

Memory leak after upgrading from 1.9.5 to 2.4.0 #4871

noveogroup-amorgunov opened this issue Mar 3, 2025 · 2 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

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

3 participants