Garbage collection causes frequent spikes in performance

I recently finished a game, and after a final wave of optimization I noticed that there was periodic half-second spike, every 15 seconds. I managed to trace this issue back to the GC, as every time there was a lag spike, some memory was release. The memory usage during this 15s period of time would constantly grow, only when the update method of my app is being called.

Of course I went through the whole thing and I don’t make any new objects, functions or arrays in it, except for quite a few cc.p objects, which I understand should be optimized for JIT or something. I have a lot of cc.p() objects, because each frame, all objects on the screen need to move and there’s perhaps 20 of them. Is my only option creating less objects to being with? The V8 and Spidermonkey engines seem to handle this way better in the browsers.

Bump :cry:

You mean in JSB ?

  1. You can call GC yourself in JSB by using cc.sys.garbageCollect(), so you can manage to make GC in a frequency you want so that there won’t be very often memory usage piques
  2. There is a way to reduce cc.p object creation, you can use property APIs which won’t create cc.p object while you move elements
node.x += dx;
node.y += dy;

At last, I admit that I’m not sure what’s the difference between the behavior of SpiderMonkey in JSB and in Firefox. We should have used the same thing, because it’s compiled directly from Firefox source.

Thank you, your suggestions greatly improved the performance. Keep up the good work with Cocos2D, btw :smile:

Hi @pandamicro,

I have the same problem on iOS, after about 15 seconds I get a stop for about half a second.

Using cc.sys.garbageCollect() more often doesn’t help, you see then always a little stop.

Is there a way to stop and start garbage collection by myself?

[SOLVED] using cc.sys.garbageCollect() before each game level starts seam to work fine now.

Thanks

Michael