Phaser.Text.destroy() doesnt work? Causing Memory issues with State Restart

Recommended Posts

Howdy all


Does the Phaser.Text.destroy() method actually work?


I'm testing in v2.0.7, and I've found that by adding a just a single Phaser.Text object, if I then restart current State many times, the memory skyrockets. Yes, I'm using a shutdown method, and I'm calling destroy and setting the phaser text object to null. But no dice. If I test the same code without creating the Phaser.Text object, the restart is fine, no crazy memory usage.


I created a small test game that shows to bug reliably on my PC, can be seen here: http://phasertext.parseapp.com/


Two options are available, you can start the Game State with or without a Phaser.Text object being created.

The Game State then has a 5 second timer and will do a restart 10 times.

Little bit of info in the Console Log.


Testing in Firefox and checking its total memory usage:

- Without Phaser.Text memory bounces between 350 to 450mb during the 10 restarts.

- With Phaser.Text memory skyrockets to 2 gig after 10 restarts and never comes back down until the tab/window is closed. Once closed, GC eventually kicks in and the memory settles back down to 300mb.

Before and after each test, I shutdown Firefox. During the test I only have the one tab open running the game.


The Game State code is below.

The creation of a Phaser.Text object is driven by the setting of boolean BasicGame.createText when the Game State is started from the MainMenu (MainMenu code not shown, can be seen in test app via debug).

BasicGame.Game = function (game) {	//	When a State is added to Phaser it automatically has the following properties set on it, even if they already exist:    this.game;		//	a reference to the currently running game    this.add;		//	used to add sprites, text, groups, etc    this.camera;	//	a reference to the game camera    this.cache;		//	the game cache    this.input;		//	the global input manager (you can access this.input.keyboard, this.input.mouse, as well from it)    this.load;		//	for preloading assets    this.math;		//	lots of useful common math operations    this.sound;		//	the sound manager - add a sound, play one, set-up markers, etc    this.stage;		//	the game stage    this.time;		//	the clock    this.tweens;	//	the tween manager    this.world;		//	the game world    this.particles;	//	the particle manager    this.physics;	//	the physics manager    this.rnd;		//	the repeatable random number generator    //	You can use any of these from any function within this State.    //	But do consider them as being 'reserved words', i.e. don't create a property for your own game called "world" or you'll over-write the world reference.};BasicGame.Game.prototype = {	textStyle: null,	phaserText: null,	restartCounter: 0,	create: function () {		this.game.physics.startSystem(Phaser.Physics.P2JS);		this.game.physics.p2.updateBoundsCollisionGroup();		this.game.physics.p2.setBoundsToWorld(true, true, true, true, false);		this.game.physics.p2.setImpactEvents(true);		this.game.physics.p2.gravity.y = 500;				this.add.sprite(0, 0, 'gamepage');				//create a bunch of P2 boxes to cause lots of collisions and put a bit of load on the game		this.boxes = this.game.add.group();		for (var i = 0; i <= 20; i++) { 			var box = this.game.add.sprite(100, 100, 'box');			this.game.physics.p2.enable(box);			this.boxes.add(box);		}		this.textStyle = { font: "24px Verdana", fill: "#000000", align: "center", stroke: "#000000", strokeThickness: "2" };				if (BasicGame.createText) {			this.phaserText = this.game.add.text(100, 150, 'Phaser.Text', this.textStyle);		}				if (this.restartCounter < 10) {	//do 10 timer based restarts			this.game.time.events.add(Phaser.Timer.SECOND * 5, this.restartFromTimer, this);		} else {			console.log('*** All timer based restarts completed ***');		}	},		update: function () {	},			restartFromTimer: function() {		this.restartCounter++;		console.log("*** Timed restart number: " + this.restartCounter);		this.restartGame();	},	quitGame: function (pointer) {		this.state.start('MainMenu');	},		restartGame: function (pointer) {		this.state.restart();	},		shutdown: function() {		console.log("Shutdown");		this.textStyle = null;		if (this.phaserText != null) {			console.log("phaserText destroy");			this.phaserText.destroy();			this.phaserText = null;		}		this.boxes.removeAll();	}};

Share this post

Link to post
Share on other sites

Issue #1162 opened.


I'm really hoping this is the source of the memory issue. I'm close to finishing a game, and after adding some music the restart became fatal on a phone (PC fairs better). My shutdown method is now 258 lines long and I've ran out of things to destroy()...Argh... Ultimately the only thing that fixed the issue was when I removed all use of Phaser.Text objects from the game. So then I made this simplified test case just to better pinpoint the issue.

Share this post

Link to post
Share on other sites


I had same problems.






then this  works :
PIXI.Texture.prototype.destroy = function(destroyBase)
    if (destroyBase) this.baseTexture.destroy();
    this.valid = false;
hope this helps.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.