简体   繁体   English

Javascript内存管理(requestAnimationFrame回调)

[英]Javascript memory management (requestAnimationFrame callback)

I am trying to make a blatant rip off of asteroids, but I'm running into a serious memory management problem. 我正试图从小行星上扯皮,但是我遇到了一个严重的内存管理问题。

I'm using window.requestAnimationFrame to serve up frames for my game by having a "gameloop" function call requestAnimationFrame with itself as an argument. 我使用window.requestAnimationFrame为我的游戏提供框架,方法是使用“ gameloop”函数调用requestAnimationFrame并将其作为参数。 However, I must be doing something very wrong because the memory usage of my game is massive which results in frequent garbage collection and poor performance (and the typical saw-toothed memory usage pattern associated with it). 但是,我肯定做错了什么,因为我的游戏的内存使用量很大,导致频繁的垃圾回收和较差的性能(以及与之相关的典型的锯齿状的内存使用方式)。 I'm pretty sure that the reason for this problem is that many function objects are being created by this loop of function and callback - probably some innocuous statement which results in a function being created instead of being reused... probably the gameloop function... 我很确定,这个问题的原因是函数和回调的循环正在创建许多函数对象-可能是一些无害的语句,导致该函数被创建而不是被重用...可能是gameloop函数。 ..

I got this idea from this post: https://www.scirra.com/blog/76/how-to-write-low-garbage-real-time-javascript 我从这篇文章中得到了这个想法: https : //www.scirra.com/blog/76/how-to-write-low-garbage-real-time-javascript

Here's a jsfiddle with my code on it: http://jsfiddle.net/LEqUr/ (hit space with the window in focus to make new asteroids appear). 这是上面写有我的代码的jsfiddle: http : //jsfiddle.net/LEqUr/ (将焦点对准窗口的空白区域使新的小行星出现)。 I know some of my stuff is camel case and some is done with underscores - sorry. 我知道我的某些东西是骆驼套,有些是用下划线表示的-对不起。

Here's (I think) the most relevant part of the code: (我认为)这是代码中最相关的部分:

var gameLoop = function () {
    getSetStageSize(1, 1);
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    updateGameState();
    window.requestAnimFrame(gameLoop);
}

window.requestAnimFrame = (function () {
    return window.requestAnimationFrame || 
       window.webkitRequestAnimationFrame || 
       window.mozRequestAnimationFrame || 
       function (callback) {
               window.setTimeout(callback, 1000 / 60);
           };
})();

So with all that said, I'd really like some feedback about how my code is organized (it could definitely use some work), some information about callbacks, some information about how/when objects are created within crazy nested callbacks and any other helpful advice you might have. 综上所述,我非常希望获得一些有关代码组织方式的反馈(它肯定可以使用某些工作),有关回调的一些信息,有关如何/何时在疯狂的嵌套回调中创建对象的信息以及任何其他有用的信息。您可能有的建议。

Thanks! 谢谢!

I don't know too mich about the animation-specific parts but you could simplify the last part a bit. 我对特定于动画的部分不太了解,但是可以稍微简化一下最后一部分。 You don't need the IIFE because you aren't declaring any local variables. 您不需要IIFE,因为您无需声明任何局部变量。

window.requestAnimFrame = (
    window.requestAnimationFrame || 
    window.webkitRequestAnimationFrame || 
    window.mozRequestAnimationFrame || 
    function (callback) {
        window.setTimeout(callback, 1000 / 60);
    }
);

Other than that, you might wat to consider coding things to use setInterval behind the scenes instead of setTimeout. 除此之外,您可能会考虑在后台使用setInterval而不是setTimeout进行编码。 When you use setTimeout, the actual time per frame is the (100/60) from the timeout plus however long it takes for your code to process that frame. 使用setTimeout时,每帧的实际时间是超时后的(100/60),再加上代码处理该帧所花费的时间。 setInterval, on the other hand, is more aware of the real clock behind the scenes and will adjust the timeout between frames to compensate for the processing time. 另一方面,setInterval更了解幕后的真实时钟,并将调整帧之间的超时以补偿处理时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM