Memory build-up using font:drawTextAligned at high setRefreshRate()


My recent arcade-style games make heavy use of

I am seeing memory build-up on my game over screen, where something is not being caught by the built-in garbage collector.

At first I called it a leak (I may still do so in the text below) but my hunch is garbage collection is not being called for some reason, as the build-up does not happen in my main game state.


This only happens at "high" setRefreshRate() of 46 or greater

I define my fonts as follows:

local fntScore ="Fonts/LCD")
local fntMessage = gfx.getSystemFont(gfx.font.kVariantBold)

Then I draw text to the screen:

these leak:

  1. fntScore:drawTextAligned("TEXT", 0, 0,, 2)
  2. fntScore:drawTextAligned("TEXT", 0, 0)

But interestingly alternative uses do not leak:

these do not leak:

  1. fntScore:drawText("TEXT", 0, 0, 2)
  2. fntMessage:drawTextAligned("TEXT", 0, 0,, 2)


  • any fonts loaded using are affected
  • is not affected

Rate of leak

Running at 50fps the memory leak grows very fast!
Witness the leak from one call to
(and I had a handful or more depending on the state of the game)

Screen shot 2022-06-01 at 22_57_39


  • only happens at setRefreshrate(46) or higher
  • does not happen in my game state when other things are going on


I have found several workarounds:

  1. replace all fntScore:drawTextAligned with fntScore:drawText (doable in my game but perhaps not in other games)
  2. add collectgarbage("collect") at the end of my update() loop
  3. run at setRefreshRate(45) or lower

See: Memory Leak in SDK