Random slow downs in app

If you’ve played my game, you’ll notice that there will occasionally be random times when the FPS will drop to about 2 FPS. I’ve spent a lot of time investigating this, without much luck. My guess is that it might be Lua attempting to collect garbage, which interferes with my game. I’ve seen it happen with other apps before. If anyone has any idea what this issue is and how to fix it, any help would be great.

Have you tried collecting garbage yourself with gc() to spread the load a bit?

I haven’t played your game @zoyt but I’ve had a couple issues like this myself, is there a link to it?

@Zoyt one way to deal with this is to pre-allocate a pool of objects and keep reference to them.

You never “destroy” an object, instead, you put it in your pool. Whenever you need to create a new object you take it out of the pool and use it. This means that Lua will never garbage collect the memory because it is always in use — either actively in your game, or inactive in your memory pool.

@Simeon - so if we’re talking about physics objects, I assume you’d add the objects you didn’t want to a table, then remove from this table when you needed an extra object.

You can actually destroy the physics objects (body:destroy() http://twolivesleft.com/Codea/Reference/Physics.html#body.destroy ) without causing Lua to garbage collect. (Not completely certain, @John can probably confirm this.)

So you might keep your objects in a pool. Create and attach a physics body when you need to use them, then destroy and detach the physics body before you put them back in the pool.

@Simeon - I thought you meant not to destroy the physics objects, so I did an example with just physics objects, one version destroying them, the other putting them in a table for re-use.

There is no difference in speed at all. Code here


Long press. Drag the tab you want to run, to the right.

PS I modified it to use just table objects instead of physics objects, and it didn’t see to make much difference.

@zoyt i had this problem a lot with some of my libs i published here. I usually managed to solve it by putting a collectgarbage() in the code everywhere after i dereference some object like an image or a mesh or a big table. Otherwise Lua waits to much to activate the collector, and then it generates a lag. Also check you have not other apps running in the background: sometime a webpage can generate such lags (i suspect, but not demonstrated)

Thank. I’ll try some of these options.