redux-toolkit
redux-toolkit copied to clipboard
Memory leak after upgrading from 1.9.5 to 2.4.0
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:
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?
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.
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;
Thanks for feedback, I will try to debug our code and provide more data
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
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 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