Jump to content

Phaser tweens cause memory leaks?


johann_dev
 Share

Recommended Posts

I'm working on a game and I've noticed it drops 4-5 frames on very regular intervals. I read an article that suggests it could be a "Memory Leak", in that the garbage collector is coming by and cleaning up dereferenced objects and that's what's slowing the game down.

I ran the chrome profiler and this pattern of my Heap usage does suggest that's what's happening:

e507d9653150ef27c9728a080b423152.png

 

So I'm trying to pool all my objects but the problem I have is with Tweens. I use a ton of them for juicing my UI text and whatnot so I create them on the fly very often. I tried reusing tweens instead of creating new ones, but in my tests I've noticed that it doesn't make a difference. I suspect it's because even if I reuse a tween, it's still creating tweenData objects under the hood, and that they're leaking memory.

This is the example code I used to show that reusing a tween (using one at all?) causes memory leaks:

var gameWidth = 704;
var gameHeight = 396;

var game = new Phaser.Game(gameWidth, gameHeight, Phaser.CANVAS, "game", {
  init: init,
  preload: preload,
  create: create,
  update: update
});

var fullscreenKey;

var snes;
var speed = -300;
var tween;

function init() {

  game.scale.pageAlignHorizontally = true;
  game.scale.pageAlignVertically = true;
  game.renderer.renderSession.roundPixels = true;
  game.camera.roundPx = true;
  game.stage.backgroundColor = '#facade';
  Phaser.Canvas.setImageRenderingCrisp(this.game.canvas);

  game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;

};

function preload() {

  game.load.image('snes', 'https://s8.postimg.cc/csffa5txx/snescontroller.png');

}

function create() {

  snes = game.add.sprite(game.camera.view.width / 2, game.camera.view.height / 2, 'snes');
  snes.anchor.setTo(0.5);

  tween = game.add.tween(snes.scale).to({
    x: 2,
    y: 2
  }, 50, Phaser.Easing.Linear.None, true).yoyo(true);

  game.time.events.loop(100, addTween, this);

  game.time.events.start();

}

function update() {

}

function addTween() {
  tween.start();
}

and the heap: 

 

a91db068d8fd714803ad51b803d3996b.png

 

Am I mistaken? Is it something else? Are my tweens slowing my game down because the garbage collector has to clean them up?

Link to comment
Share on other sites

That's not a memory leak, but frequent garbage collection, which is normal but unwanted.

Chrome can show you where in the code most memory allocations are made.

If a tween has completed naturally you can call start again without to/from. That will restart it without creating a new data timeline.

You can use generateData to build a list of values and reuse it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...