Jump to content

CPU usage/Garbage Collection advice


Meowts
 Share

Recommended Posts

Hey,

 

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

Cool thanks for the advice! All throughout (during the round and on each round reset) I am using the destroy() method (sorry I said the wrong thing there), but it could be that there are cases where I could be using kill() instead. I'll play around with that!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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