Is there any way to "commit" the state in Redux to free memory?
Asked Answered
P

1

5

I'm working on a real-time multiplayer game and I use Redux on both the server and the client to store the state of the application.

However, the amount of actions dispatched into the store is significantly higher than in an usual application because my game is real-time. I suspect that this is why Redux is using a lot of memory.

To my understanding Redux stores all actions dispatched into a store in memory in order to be able to do its "time traveling". I also noticed that the Redux DevTools allows you to commit the state.

What I would like to do is commit the application e.g. every 10 seconds to save memory. I never have the need to go back more than 10 seconds in my application anyway so storing all actions seems unnecessary, even for debugging purposes.

Does Redux support this? If not, is there any way to achieve this behavior?

Thank you in advance!

Persia answered 25/2, 2016 at 12:37 Comment(2)
Are you using any redux logging middleware?Infectious
I'm using the Redux DevTools in the client application during development. However the problem described above also occurs in production where there is no additional middleware in use.Persia
B
7

Note that while the Redux DevTools do store the history of actions to enable the time-travel debugging feature, Redux itself does not - it only keeps a reference to the current state. The DevTools also have some additional overhead due to rendering the list of actions and store contents.

Beyond that: what makes you say that Redux is "using a lot of memory"? The only memory that Redux uses is whatever is needed to represent the store state. Unless you have some particular benchmarks that actually show memory problems, I wouldn't see it as a meaningful concern.

Brolly answered 25/2, 2016 at 17:18 Comment(6)
So what you are saying is that Redux only stores the current state in memory, nothing else? If so, that's good news. :)Persia
Unless you still have Redux dev tools running in production or you have references to old versions of the state lingering in your code. While redux doesn't keep the old state around, other things might.Thibodeaux
This answer is correct. Redux does not keep the old states around, but DevTools does until you “commit”.Zurheide
Alright thanks for clearing this up. This is good news, because Redux has worked really well for keeping track of the game state on both the client and the server. And keep up the great work @DanAbramov!Persia
I am glad this is the case too. The Dev tools give a skewed image of how redux works and having a copy of each state with 1000s of records that a redundant worried me a bit (specially for mobile). How does redux allow you to go back in history then if the previous state only records references. If there the previous state has a record that is not present in the current state and you would go back in history, that record would be gone? Or , again, does Devtools give me an incorrect image of redux? Can you actually go back in history without devtools?Headrick
No, "time-travel debugging" as you're seeing it is explicitly a development feature, same as when you compile a C++ program in debug mode and it includes all kinds of extra info in the output. Now, Redux's architecture does make it possible to implement something like that yourself for use in something like an undo/redo feature, but yeah, in production you're not going to be jumping back and forth like that, the application is just running as-is.Brolly

© 2022 - 2024 — McMap. All rights reserved.