Can a Redux store lead to a memory leak?

前端 未结 4 2468
囚心锁ツ
囚心锁ツ 2021-02-20 00:55

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 ther

相关标签:
4条回答
  • 2021-02-20 01:24

    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.

    0 讨论(0)
  • 2021-02-20 01:28

    "Since Redux creates a new store every time the data charges and keeps the old ones."

    Vanilla Redux does not do that, or every Redux store would leak. As it is, the rest of your application holding reference to the state will prevent it from being cleaned up.

    Eg something like

    window.states = []
    store.subscribe(() => {
      window.states.push(store.getState())
    })
    

    Will cause unbounded memory growth.

    Also, some Redux development tools do leak to provide time travel capabilities, so be sure they are turned off for your production build.

    0 讨论(0)
  • 2021-02-20 01:44

    It is not recommended to pile up 2MB of data /second on the user's browser. Redux store is a client-side on the browser. The other part of the question regarding memory leaks don't happen as far as I know. Some useful links are:

    Diagnose memory leaks using Chrome devtools

    Four types of memory leaks to watch out for

    0 讨论(0)
  • 2021-02-20 01:46

    First, that does sound like an awful lot of data. Does your client application actually need that much data that often?

    Second, Redux does not "create new stores". Assuming you are following the recommended approaches for updating data, old data references are discarded and will be garbage-collected. Redux itself does not keep around references to the old state trees by default, although the Redux DevTools do in order to enable time-travel debugging.

    You may want to read through several sections in the Redux docs. In particular, see http://redux.js.org/docs/faq/Performance.html , http://redux.js.org/docs/recipes/StructuringReducers.html .

    You may also want to browse through my Redux addons catalog, which includes addons that can do things like batching updates.

    0 讨论(0)
提交回复
热议问题