Garbage collection causing lag but only at 30fps


I have a couple very large tables and I noticed in the profiler that garbage collection is spiking because of it every now and then. This was causing the frame rate to drop below 30 but when I set playdate.display.refreshRate(0) it doesn't have the same impact and the frames stay at 50 (full screen redraw).

Does it have something to do with how the frame limiter works behind the scenes? My guess was that maybe the frame limiter was taking into account my playdate.update() time when setting a delay but not the garbage collection time?

In the past i have also seen GC issues only at specific frame rates. It was more to do with available time at that update rate, rather than the frame limiter.

Have you tried managing garbage collection manually? It's how I worked around it. It's a Lua feature:

  • collectgarbage("collect")

You can also measure it and see how much is being collected.

Of course the best solution is to find the cause of the garbage and change your code to produce less/none.


Thanks for the tip! I will definitely be trying manual garbage collection now.

Is garbage only produced when you have something in memory and then stop referencing it? The large tables I had were not being unreferenced but when I tested with smaller tables the garbage collections spikes went away. That article describes it as a mark and sweep so the only other thing I can think of is that my table is so big that the marking part takes a while to check it for garbage even if there isn't any?

Quite possible! Would need some investigation to say for sure