Jump to content

CPU usage/Garbage Collection advice


Recommended Posts



To start, I'm still fairly inexperienced with game development, TBH.


I'm building a game right now that involves rounds, and replaying of rounds if the user chooses. Each round I have various JS objects being created, with sprites, sound, text, etc. The issue is, when rounds are being replayed, the CPU usage keep rising - sometimes up to 50%. Each time a round replays, I'm killing the sprites along with all their children.


To describe the architecture, it's something like this:


JS Object -> has other objects -> each have sprites -> has elements (audio, text, buttons)

The one object that has the bulk of the other objects aggregated is re-instantiated each round replay


I know that's not a ton of info (gotta run, I can elaborate) - is there anything that comes to your mind as to what might be eating up processing power? Any sort of, "Clear every process, f' the rest of em!" function that's built into Phaser?






Link to comment
Share on other sites

Using destroy() on a sprite will remove it and set it for garbage collection. The garbage collector is unpredictable and could slow down your game once it does it's job.  kill()  on the other hand will not set the object for garbage collection, but will keep the memory allocation in case you create a new sprite, then this object will be recycled.


If you are creating many objects in your game and they get destroyed a lot (example spaceship game where you are killing lots of enemies and collecting lots of stuff) it's better to use kill() as it saves the browser having to create new objects and garbage-collect the dead ones all the time. Instead, it reuses the killed (kill() ) objects over and over saving processing power and memory.


Without seeing your code it's hard to find the exact cause, but the other thing you want to avoid is having too much stuff in update() as this gets called dozens of times per second.

Link to comment
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.
  • Create New...