JavaScript 在 Chrome 中卡住/崩溃

标签 javascript google-chrome canvas web-worker

这是 fiddle :http://jsfiddle.net/36mdt/

大约 10-20 秒后,显示屏在崩溃后不久开始随机卡住。我无法在 Firefox 中重现这一点。

分析显示没有异常。

http://jsfiddle.net/3pbdQ/显示肯定存在内存泄漏。即使是 1 FPS,内存使用量也会达到每帧 5 兆字节。

顺便说一句,这个例子确实展示了 Math.random() 并不是那么随机。

最佳答案

我只做了 2 项性能改进,5 分钟后它没有崩溃(似乎也没有泄漏内存)。结帐http://jsfiddle.net/3pbdQ/3/

  1. 不在每次迭代中计算大小
  2. 使用超时而不是卡住间隔。
  3. 使用位运算符对数字取底

Profiling reveals nothing unusual.

Chrome Profiler 不适用于 WebWorkers,据我所知。根据与 Paul Irish 的对话:

"Check about:inspect for shared workers, also you can do console.profile() within the worker code (I THINK) and capture those bits. The "cleans up" is the garbage collector: if after the cleanup there is still a growing line of excess memory, then thats a leak."

On a side note, this example really shows how Math.random() is really not so random.

众所周知,没有完美的随机算法,但无论如何,您看到的一堆分组颜色是因为您没有设置 canvas.height 和 canvas.width,它与 CSS 值不同。

编辑:仍在泄漏内存,我不知道为什么,大约 10 秒后它“清理”了。超出我的知识范围,但在 60 FPS 下运行流畅(var TIME = 16)

关于JavaScript 在 Chrome 中卡住/崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11036765/

相关文章:

javascript - 如何判断 Canvas 动画何时结束

javascript - CORS:PHP 绕过不起作用

javascript - 类型错误 : object is not a function in node Js

javascript - 为什么 localStorage 不持久存在于 Chrome 中?

google-chrome - 如何使用谷歌浏览器中的键盘在标签之间移动标签

javascript - 检查给定点周围所有点的有效方法

javascript - 内容出现在 Firefox 中,但不出现在 Chrome 中

javascript - polymer 数据绑定(bind)到方法

javascript - 对 SlickGrid 编辑器工厂的期望是什么?

html - 在没有 CSS 的打印模式下,将 HTML 文档设置为在每个表格之间分页