Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Redux store lead to a memory leak?

I have a dashboard application with several charts getting updated on a set interval. My first thought was to update the data in the store and then let all charts feed from there.

But could that lead to a memory leak? Since Redux creates a new store every time the data changes and keeps the old ones. Would a ~2mb data every second pile up and crash the application?

The alternative I see is to keep the data in the local state (with setState). I hope some more experienced React/Redux devs can advice me on this. Thanks!

like image 397
nauti Avatar asked Oct 09 '16 12:10

nauti


People also ask

What is the main cause of memory leaks?

DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed.

Does Redux use more memory?

First, in terms of raw memory usage, Redux is no different than any other JavaScript library.

Is Redux store in memory?

What is the redux store? In an object that stores the state tree of the app, this data is saved while are in the same tag session, if you refresh the tag of your browser all the state tree restarts, so all the info before is no longer in memory.


1 Answers

Dan Abramov, the creator of Redux addresses this concern here like so:

Note that sometimes people get confused about Redux and assume that on every action, the state tree has to be cloned deeply. This is absolutely not the case. Only the parts that changed need to change their references. For example, if an action causes a change to one item in an array, indeed, that item and the array will need to be copied, however, all other elements in the array will keep their identities. Because most of the times actions are very targeted and affect a few state keys, and because Redux encourages normalizing data so that the data structures are not deeply nested, this is much less of a problem for typical webapps than one might imagine.

I think this is the meat of the answer.

like image 162
nikjohn Avatar answered Sep 19 '22 18:09

nikjohn