Triple-Check this is really memory bloat and not just unnecessary re-rendering/re-computation
Mutating state in Redux is almost always a really bad idea as it's a sort of prerequisite for the library. My first concern would be to triple-check that you are indeed running into memory issues due to memory bloat and not due to unnecessary re-renders or due to unnecessary computations. By this I mean, if you are keeping huge amounts of data in the same place, make sure you are not doing something that causes React to unnecessarily re-render things or sift through too much data.
You can solve that issue by judicial use of the reselect library or by using some other type of memoization that will retrieve data from your reducers in a manner that avoids unnecessary recomputation. Similarly, make sure you aren't unnecessarily re-rendering every item in your entire list just when changing a single row.
Get rid of references to previous state
To get something to be garbage compiled in JavaScript, you just make sure that nothing is referencing it any longer.
If you really need to go down this path, it's essential that you don't keep the old page's data "alive" if you will because JavaScript only garbage collects things that are no longer referenced.
Vanilla Redux is not holding on to previous states under the hood. All it's doing is keeping the current state in a let
variable and then changing its value after getting the new state from dispatching the action to the root reducer (see source code).
So, I would ensure I''m not using something like redux dev tools which persists previous state.
Then in the reducer, create a new object that doesn't use the previous data in any way, but rather returns a new object with the new data:
const data = (state, action) => {
switch (action.type) {
case 'RETRIEVE_DATA_SUCCESS':
return action.payload;
default:
return state
}
}
I suggest reading up on Chrome's memory profiling tools here to make sure this is working correctly.