Jump to content

Memory Issues


Edricus
 Share

Recommended Posts

Hello everyone. So I am building a pretty basic game and thus far everything has gone fine. However now I am just doing some performance enhancement on the game. I noticed something very weird. Upon loading the game memory spikes up to almost 20 MB then GC kicks in and drops to around 9.0 or so MB. This is all while just idling at the menu. Here is a screenshot of the memory log in Chrome. Also I have pasted the code that runs from my Boot, Preload and Menu states. Hopefully someone can point out something blatantly obvious that I have missed. :S

 

post-9024-0-22829700-1403107255_thumb.pn

'use strict';function Boot() {}Boot.prototype = {  create: function()   {  	// Set max number of touch inputs    this.game.input.maxPointers = 1;    // Start the next state    this.game.state.start('preload');  }};module.exports = Boot;
'use strict';function Preload() {  this.ready = false;}Preload.prototype = {  preload: function()   {    // This event is dispatched when the final file in the load queue has either loaded or failed    this.load.onLoadComplete.addOnce(this.onLoadComplete, this);    // Load all assets for the game    // Title Screen Assets    this.load.image('background', 'assets/background_title.png');    this.load.image('playBtn', 'assets/play_btn.png');    // Game Screen assets    this.load.image('mouthBackground', 'assets/mouth_background.png');        this.load.image('mouthwash', 'assets/mouthwash.png');    this.load.image('toothpaste', 'assets/toothpaste.png');    this.load.spritesheet('tooth', 'assets/tooth.png', 80, 95);    this.load.spritesheet('bactoSurfer', 'assets/bactoSurfer.png', 34, 24);    this.load.spritesheet('sharkBacteria', 'assets/sharkBacteria.png', 36, 45);    this.load.spritesheet('octoBacteria', 'assets/octoBacteria.png', 36, 45);    // Game Over Assets            this.load.image('scoreboard', 'assets/scoreboard.png');    this.load.image('gameover', 'assets/gameover.png');    this.load.image('startButton', 'assets/start-button.png');    this.load.image('exitButton', 'assets/exit-button.png');  },  update: function()   {    if(this.ready === true)     {      this.game.state.start('menu');    }  },    onLoadComplete: function()   {    this.ready = true;  }};module.exports = Preload;
'use strict';function Menu() {}Menu.prototype = {  create: function()   {    // Add the background sprite    this.background = this.game.add.sprite(0, 0, 'background');    // Play Button    this.startButton = this.game.add.button(this.game.width * 0.5, 350, 'playBtn', this.startClick, this);    this.startButton.anchor.setTo(0.5, 0.5);  },  startClick: function()   {    this.game.state.start('play');  }};module.exports = Menu;
Link to comment
Share on other sites

One thing you should do in all your states, if you create objects using game.add and keep a reference of them in a state property (like you did in the Menu state for background and startButton). You should add a shutdown() function where you mark those attribute as null:

shutdown: function() {  this.background = null;  this.startButton = null;}

That's because each time you start the state, the create method will be called but when you start another state, the current state is kept in memory (inside game.state). I found it to be quite useful when my states create many objects and keep reference to them.

Link to comment
Share on other sites

One thing you should do in all your states, if you create objects using game.add and keep a reference of them in a state property (like you did in the Menu state for background and startButton). You should add a shutdown() function where you mark those attribute as null:

shutdown: function() {  this.background = null;  this.startButton = null;}

That's because each time you start the state, the create method will be called but when you start another state, the current state is kept in memory (inside game.state). I found it to be quite useful when my states create many objects and keep reference to them.

 

Thanks for advice. I'll make sure to add that to all my states. I also just realized that the memory recording I took in the original post is not accurate. I just closed my browser and re-opened it and started a recording from about:blank going to a localhost I set up to test my game. Below is a screenshot of the current memory reading as of loading the menu and allowing it to idle there for about a minute or two. All the code from the original post is still the same with the small addition of the shutdown function suggested by haden. I was wondering however, since this is my first time using this sort of profiling tools, if this type of reading is normal for what I am currently seeing on the screen as well as the code provided above.

 

Edit: Screenshot removed as per request.

Link to comment
Share on other sites

Yep that's fine.  

 

Also try disabling your Chrome addons, running in incognito mode and not having other Chrome windows open.  Those things can contribute to inaccuracies.

 

Also I've found when a game is run in webGL the memory seems to work quite differently than with Canvas and you're likely to see a much more clean and even memory graph.  So temporarily try forcing the game to run in canvas (wherever you init your Phaser.game, change Phaser.AUTO to Phaser.CANVAS), you may like what you see.   ;)

Link to comment
Share on other sites

Yep that's fine.  

 

Also try disabling your Chrome addons, running in incognito mode and not having other Chrome windows open.  Those things can contribute to inaccuracies.

 

Also I've found when a game is run in webGL the memory seems to work quite differently than with Canvas and you're likely to see a much more clean and even memory graph.  So temporarily try forcing the game to run in canvas (wherever you init your Phaser.game, change Phaser.AUTO to Phaser.CANVAS), you may like what you see.   ;)

 

Awesome thanks for the tip Yora! So I closed all windows and ran it in incog, switched to Canvas and these are my results. Check out the screenshot below. Roughly 1 to 2 MB difference. Also I tried zooming in on the drops assuming they were GC Event calls but I couldn't find anything that tells me what that is. I assumed it was GC freeing up memory. My first thought was, that's what it is so lets zoom in and see what it freed up. Upon checking (you'll see highlighted on the right) I found it only freed 62.1KB of memory and if you really look hard at the selected section you'll see a TINY TINY drop. Nothing in the event's fired section shows what the larger drop is. Would anyone happen to be able to explain that?

 

Edit: Screenshot removed as per request.

Link to comment
Share on other sites

There is nothing to explain : you are using a framework.

 

Even if your code do nothing, the framework itself do the stuff for you : creating/updating/deleting object.

Every time an object/function/array is created, instantiated, released, or modified (adding/deleting properties for example) the object is submitted to the garbage collector.

 

Anyway, is it really an issue ? The memory usage is stable enough and should not cause freeze on the fps.

Link to comment
Share on other sites

There is nothing to explain : you are using a framework.

 

Even if your code do nothing, the framework itself do the stuff for you : creating/updating/deleting object.

Every time an object/function/array is created, instantiated, released, or modified (adding/deleting properties for example) the object is submitted to the garbage collector.

 

Anyway, is it really an issue ? The memory usage is stable enough and should not cause freeze on the fps.

 

It's definitely not an issue now that I understand what's going on. I appreciate you taking the time to explain it a bit. Seeing as this is my first time making a web-based game and using an engine such as Phaser I've never really done a lot of memory profiling before with this much depth. I've been learning a lot of the stuff myself over the past few weeks, so I just wanted to make sure the things I'm seeing are normal and that there is nothing to worry about. Once again thanks to all the replies.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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