Garbage Collection Profiling



  •  

    Hi,

    I've been working hard to optimize my code, but I'm struggling with the 6 - 36 CPU that is shaved off of my bucket each tick, supposedly by Garbage Collection(GC).  Random parts of my code (like if statements) start to consume 20CPU in a tick, or if not it shows up at the beginning of the tick.

     I would like to profile my code using something like:

    https://github.com/matthieu/node-gc

    https://github.com/dainis/node-gcstats

    but I'm not certain how I could get these modules to load from within the screeps environment.  Could we have a feature that allows us to better understand how GC is affecting our CPU usage?

    Much appreciated.

     


  • Culture

    There is no way garbage collection is taking that much CPU time. How much memory are you using? You have to json parse that every tick.

    You should consider using the screeps profiler to focus on your code, rather than trying to figure out what is going on with V8. Since those modules you're looking at require C code to be compiled in as well I am pretty sure you'll never get them working.



  • Currently using about 400k of memory, normal JSON.parse on a private server takes between 8-12CPU/tick from what I have seen.  I get the CPU at the very beginning of the main loop, and check it again immediately after the first load from Memory.  This should be giving me a pretty accurate representation of the parse costs.

    What I am seeing though is an extra 15-20CPU scattered randomly throughout the main loop, sometimes in the middle of a creep's behaviour, sometimes lumped in with Memory parsing or other areas.  I can only reasonably assume this is GC, unless you have an alternative explanation.

    Understandably getting C code working inside the Screeps environment may not be possible currently, but I would really appreciate some better way of profiling my code.  I have heard that it's possible other player's GC can affect the runtime of our code, is that true?


  • Culture

    You're using an insane amount of memory for someone who is only GCL6. For context I'm GCL15 and am using about the same as you are.

    This could be what is triggering your garbage collection issues- json.parse of lots of data each tick is more likely to trigger another GC during your tick since you're creating a bunch of new objects.


  • Culture

    I agree with tedivm, I use about 700k with GCL 35. I'd suggest looking into reducing memory object count. The more objects you have in memory the more memory it costs. It's not the size that counts! It's the object count.



  • Thanks for pointing me in the direction of Memory.  Initially halving my memory footprint reduced my average usage by about 15CPU/tick, I had no idea that utilizing even 400k would cause such a significant impact on performance.