requestAnimationFrame垃圾收集 [英] requestAnimationFrame garbage collection

查看:149
本文介绍了requestAnimationFrame垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Chrome Dev Tools v27中使用时间轴分析了以下代码的内存使用情况。
$ b

 <!DOCTYPE html> ; 
< html>
< head>
< meta http-equiv ='content-type'content ='text / html; charset = UTF-8'/>
< title> RAF< / title>
< / head>
< body>
< script type ='text / javascript'charset ='utf-8'>
var frame = function(){
window.webkitRequestAnimationFrame(frame);
};
window.webkitRequestAnimationFrame(frame);
< / script>
< / body>
< / html>

注意这很简单。但最终我看到出现了一个指示垃圾回收器正在回收内存的齿形。





默认情况下,raf是否创建垃圾对象?有什么办法可以避免这种情况? Thx。

解决方案


我发现了以下内容:
如果您将RAF函数更改为两个乒乓功能,您可以获得更少的垃圾。你无法避免第一个最初的大GC,但在此之后,你只能看到约50kb的小GC,而不是700kb-1mb的GC。代码如下所示:

 < script type ='text / javascript'charset ='utf-8'> 
window.frameA = function(){
window.webkitRequestAnimationFrame(window.frameB);
};
window.frameB = function(){
window.webkitRequestAnimationFrame(window.frameA);
};
window.webkitRequestAnimationFrame(window.frameA);
< / script>

我想这是您在Chrome中可以做的最好的选择。
我注意到在FF中gc的间隔或内存几乎没有变化,所以它可能与chrome调试相关(请参阅上面链接的chrome错误报告了解更多细节)。然而,当我这样部署RAF时,我注意到了我自己的游戏改进 - 而且我需要能够调试它,而不需要在普通用户机器上不会发生的人工GC。


I'm profiling the following code's memory usage using the Timeline in Chrome Dev Tools v27.

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv='content-type' content='text/html; charset=UTF-8' />
  <title>RAF</title>
</head>
  <body>
    <script type='text/javascript' charset='utf-8'>
      var frame = function() {
        window.webkitRequestAnimationFrame(frame);
      };
      window.webkitRequestAnimationFrame(frame);
    </script>
  </body>
</html>

Notice it's simple. But eventually I see the a tooth pattern appear that indicates the garbage collector is reclaiming memory.

Does raf create garbage objects by default? Is there any way to avoid this? Thx.

解决方案

I have found out the following: If you change your RAF function into two "ping-pong" like functions, you get alot less garbage. You can't avoid the first initial "big GC", but after that you see only minor GCs of about 50kb instead of 700kb-1mb GCs. The code will look like this:

<script type='text/javascript' charset='utf-8'>
  window.frameA = function() {
    window.webkitRequestAnimationFrame(window.frameB);
  };
  window.frameB = function() {
    window.webkitRequestAnimationFrame(window.frameA);
  };
  window.webkitRequestAnimationFrame(window.frameA);
</script>

I guess this is the best you can do in Chrome. I noticed that in FF the gc intervals or memory hardly changes, so its probably related to the chrome debugging stuff (see the linked chrome bug report above for more details). However, I noticed an improvement in my own game when deploying RAF like this - and heck I need to be able to debug it without artificial GCs that won't happen on normal users machines.

这篇关于requestAnimationFrame垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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