如何防止javascript堆栈溢出?
我一直在使用 javascript / jquery 构建 Conway's Life 以便在浏览器中运行它这里。 Chrome、Firefox 和 Opera 或 Safari 执行此操作的速度相当快,因此最好不要使用 IE。不过IE9还可以。 在生成新一代生命的同时,我存储了前几代生命,以便能够回顾历史。这工作正常,直到内存填满的某个点,这会导致浏览器(选项卡)崩溃。
所以我的问题是:如何检测内存何时填满?我将每一代的数组存储在一个数组中,该数组形成了各代的历史。这需要大量的内存,这会导致浏览器在几千代后崩溃,具体取决于可用内存。 我知道 javascript 无法检查可用内存量,但必须有一种方法......
I've been building Conway's Life with javascript / jquery in order to run it in a browser Here. Chrome, Firefox and Opera or Safari do this pretty fast so preferably don't use IE for this. IE9 is ok though.
While generating the new generations of Life I am storing the previous generations in order to be able to walk back through the history. This works fine until a certain point when memory fills up, which makes the browser(tab) crash.
So my question is: how can I detect when memory is filling up? I am storing an array for each generation in an array which forms the history of generations. This takes massive amounts of memory which crashes the browser after a few thousands of generations, depending on available memory.
I am aware of the fact that javascript can't check the amount of available memory but there must be a way...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑是否有办法做到这一点。即使有,它也可能是特定于浏览器的。不过,我可以建议一种不同的方式。
不是存储每一代的所有数据,而是存储每隔一段时间拍摄的快照。由于康威生命游戏是确定性的,因此您可以轻松地从给定的快照重新生成未来的帧。您可能需要保留几帧的缓冲区,以便可以使倒带顺利流畅。
事实上,这并不能真正解决问题,因为您最终会耗尽空间。但是,如果您存储每
n
帧,您的应用程序的持续时间将延长n
倍,这可能已经足够长了。我建议您对可以倒带到过去多远施加一些硬性限制,以便对必须存储的数量有一个上限。确定该帧数(30 FPS 下 10 分钟 = 18000帧
)。然后,将frames
除以您可以存储的帧数(分析各种网络浏览器以找出答案),这就是您应该使用的快照之间的间隔。I doubt that there is a way to do it. Even if there is, it would probably be browser-specific. I can suggest a different way, though.
Instead of storing all the data for each generation, store snapshots taken every once in a while. Since the Conway's Game of Life is deterministic, you can easily re-generate future frames from a given snapshot. You'll probably want to keep a buffer of a few frames so that you can make rewinding nice and smooth.
In reality, this doesn't actually solve the problem, since you'll run out of space eventually. However, if you store every
n
frames, your application will lastn
times longer, which might just be long enough. I would recommend that you impose some hard limits on how far into the past you can rewind so that you have a cap on how much you have to store. Determine that how many frames that would be (10 minutes at 30 FPS = 18000frames
). Then, divideframes
by how many frames you can store (profile various web browsers to figure this out) and that is the interval between snapshots you should use.狗伯特几乎做到了这一点。您无法确切知道有多少可用内存,但您可以知道数据集的潜在大小。
因此,获取数组中存储的每个对象的大小,乘以数组维度,这就是一次迭代的大小。将其乘以所需的迭代次数即可查看总共需要多少空间,并进行相应调整。
或者,受 Travis 的启发,只需从最后一个已知数组反向运行该模式即可。毕竟它是确定性的。
Dogbert pretty much nailed it. You can't know exactly how much available memory there is but you can know how potentially large your dataset will be.
So, take the size of each object stored in the array, multiply by array dimensions and that's the size of one iteration. Multiply that by the desired number of iterations to see how much space total it will take, and adjust accordingly.
Or, inspired by Travis, simply run the pattern in reverse from the last known array. It is deterministic after all.