游戏循环内存泄漏
我正在用 javascript 创建一个游戏,我的游戏循环每 30 毫秒调用一次,它泄漏了大量内存,因为任务管理器显示 Firefox 内存使用量在大约 20 秒内增加了 400mb。 我不熟悉如何确保在 javascript 中收集内存。
function GameLoop(tick) {
move(player1.ship);
}
function Player(name) {
this.id = 0;
this.name = name;
this.ship = Ship(this);
}
function Ship(player) {
this.pos = [1024/2, 768/2];
this.vel = [0, 0];
this.angle = 0;
this.acc = 0;
this.thrust = 0;
this.west = 0;
this.east = 0;
this.turnRate = 5;
this.player = player;
this.size = [40, 40];
this.ship = canvas.rect(this.pos[0], this.pos[1], this.size[0], this.size[1]);
this.ship.attr("fill", "red");
return this;
}
function move(ship) {
var angle = ship.angle;
var max_speed = 20;
var acc_speed = 300;
var acc = 0;
if (ship.thrust) {
acc = 0.25 * acc_speed;
}
else { //slow down
if ((acc - (0.25 * acc_speed)) > 0) {
acc -= 0.25 * acc_speed;
}
else {
acc = 0;
}
}
var speedx = ship.vel[0] + acc * Math.sin(angle);
var speedy = ship.vel[1] - acc * Math.cos(angle);
var speed = Math.sqrt(Math.pow(speedx,2) + Math.pow(speedy,2));
var speedx = ship.vel[0] + acc;
var speedy = ship.vel[1] - acc;
var speed = speedx + speedy;
if (speed > max_speed) {
speedx = speedx / speed * max_speed;
speedy = speedy / speed * max_speed;
}
ship.vel = [speedx, speedy];
ship.pos = [ship.pos[0] + speedx * 0.25, ship.pos[1] + speedy * 0.25];
ship.ship.attr({x: ship.pos[0], y: ship.pos[1]});
ship.ship.rotate(angle);
ship.angle = 0;
delete this.thrust;
delete this.west;
delete this.east;
delete old_angle;
delete angle;
delete max_speed;
delete acc_speed;
delete acc;
delete speedx;
delete speedy;
delete speed;
return this;
}
var player1 = new Player("Player 1");
setInterval(GameLoop, 30);
好的,我注释掉了一些代码,并找到了有问题的行,其
ship.ship.rotate(角度); 评论该行后,JavaScript 使用了 4500K。 知道为什么这会导致问题,以及如何在没有这段代码的情况下仍然旋转我的对象?
I am creating a game in javascript and my gameloop is called every 30ms, it leaks a lot of memory as task manager shows the firefox memory usage to increase by 400mb in about 20 seconds.
I am not familiar with how to make sure memory is collected in javascript.
function GameLoop(tick) {
move(player1.ship);
}
function Player(name) {
this.id = 0;
this.name = name;
this.ship = Ship(this);
}
function Ship(player) {
this.pos = [1024/2, 768/2];
this.vel = [0, 0];
this.angle = 0;
this.acc = 0;
this.thrust = 0;
this.west = 0;
this.east = 0;
this.turnRate = 5;
this.player = player;
this.size = [40, 40];
this.ship = canvas.rect(this.pos[0], this.pos[1], this.size[0], this.size[1]);
this.ship.attr("fill", "red");
return this;
}
function move(ship) {
var angle = ship.angle;
var max_speed = 20;
var acc_speed = 300;
var acc = 0;
if (ship.thrust) {
acc = 0.25 * acc_speed;
}
else { //slow down
if ((acc - (0.25 * acc_speed)) > 0) {
acc -= 0.25 * acc_speed;
}
else {
acc = 0;
}
}
var speedx = ship.vel[0] + acc * Math.sin(angle);
var speedy = ship.vel[1] - acc * Math.cos(angle);
var speed = Math.sqrt(Math.pow(speedx,2) + Math.pow(speedy,2));
var speedx = ship.vel[0] + acc;
var speedy = ship.vel[1] - acc;
var speed = speedx + speedy;
if (speed > max_speed) {
speedx = speedx / speed * max_speed;
speedy = speedy / speed * max_speed;
}
ship.vel = [speedx, speedy];
ship.pos = [ship.pos[0] + speedx * 0.25, ship.pos[1] + speedy * 0.25];
ship.ship.attr({x: ship.pos[0], y: ship.pos[1]});
ship.ship.rotate(angle);
ship.angle = 0;
delete this.thrust;
delete this.west;
delete this.east;
delete old_angle;
delete angle;
delete max_speed;
delete acc_speed;
delete acc;
delete speedx;
delete speedy;
delete speed;
return this;
}
var player1 = new Player("Player 1");
setInterval(GameLoop, 30);
Ok I commented out some code and have found the offending line, its
ship.ship.rotate(angle);
After commenting that line out javascript is using 4500K.
any idea why this is causing the problem, and how can I still rotate my object without this bit of code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
RaphaelJS 中的 rotate 的文档说明如下:
这听起来确实像是一个潜在的罪魁祸首。关键词是add和list。
当您旋转元素两次时, transform 函数会显示什么?我怀疑旋转的调用会积累越来越大的转换字符串。如果发生这种情况,您可以重置转换,
这应该可以解决您所看到的问题。
The documentation of rotate in RaphaelJS says the following:
That certainly sounds like a potential culprit. The critical words there are add and list.
What does the transform function show you when you rotate an element twice? My suspicion is that calls to rotate accumulate larger and larger transformation strings. if that's what's happening, you can reset the transform,
and that should clear the problem you're seeing.
我在您的代码片段中没有看到任何会泄漏内存的内容。
正如 Eugen Rieck 所指出的,Firefox(和其他浏览器)有时不会执行 GC 和/或释放它们分配的内存,直到它们确实有理由这样做。
您是否尝试过使用实际的内存分析工具来查看您的代码是否确实存在泄漏?我不确定 Firefox 是否有,但 Chrome 至少有。
I don't see anything in your code snippet which would leak memory.
As pointed out by Eugen Rieck, Firefox (and others) sometimes don't do GC and/or free memory they have allocated until they actually have a reason to do it.
Have you tried using an actual memory profiling tool to see if your code is actually leaky? I'm not sure if Firefox has one, but there's one at least in Chrome.