Redux存储会导致内存泄漏吗? [英] Can a Redux store lead to a memory leak?

查看:403
本文介绍了Redux存储会导致内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个仪表板应用程序,其中有几个图表会按设定的间隔进行更新.我的第一个想法是更新商店中的数据,然后让所有图表从那里获取.

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.

但这会导致内存泄漏吗?由于Redux每次更改数据都会创建一个新存储并保留旧存储.每秒钟〜2mb的数据会堆积并使应用程序崩溃吗?

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?

我看到的替代方法是将数据保持在本地状态(使用setState). 我希望一些经验丰富的React/Redux开发人员可以就此向我提出建议.谢谢!

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!

推荐答案

Redux的创建者Dan Abramov解决了这一问题

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

请注意,有时人们对Redux感到困惑,并认为在每次操作时都必须深深克隆状态树.绝对不是这样.仅更改的部分需要更改其引用.例如,如果某个动作导致更改了数组中的一项,则确实需要复制该项和数组,但是,数组中的所有其他元素将保留其标识.因为大多数时候动作都是针对性强的,并且会影响几个状态键,并且由于Redux鼓励对数据进行规范化,以使数据结构不会深度嵌套,所以对于典型的Web应用程序来说,这比人们想象的要少得多. >

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.

这篇关于Redux存储会导致内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆