Skip to content

Fix memory leak by caching UI fonts instead of allocating per frame#20

Open
virtualarchitectures wants to merge 4 commits into
danielcorbani:mainfrom
virtualarchitectures:fix/cache-ui-fonts
Open

Fix memory leak by caching UI fonts instead of allocating per frame#20
virtualarchitectures wants to merge 4 commits into
danielcorbani:mainfrom
virtualarchitectures:fix/cache-ui-fonts

Conversation

@virtualarchitectures
Copy link
Copy Markdown

@virtualarchitectures virtualarchitectures commented Apr 30, 2026

Fix memory leak: cache UI fonts instead of allocating per frame

Project.drawUI() was calling mainApplet.createFont() on every frame. At 60 fps this creates over 3,600 font objects per minute which was holding resources that the garbage collector couldn't efficiently reclaim. This was causing memory to climb continuously until the system ran out, even in the empty example project.

The same pattern appeared in seven other methods (createSceneButtons, createScreenButtons, addSelectSceneButton, addSelectScreenButton, createAssignDisplayButtons, createMediaButtons, createGeneratorButtons), which each allocated a fresh font object on every call.

Changes:

  • Added three PFont fields (uiFont14, uiFont16, uiFont20) to Project
  • Initialized all three once in the constructor
  • Replaced all eight createFont() call sites with references to the cached fields

NOTE: This pull request is built on top of and includes the UI updates in my other pull request #21. Apologies for not spotting this issue first. Even if you don't like the UI changes please do include the specific fixes for the memory leak in commit 80e4b61.

Before fix - Memory use rises steadily until crashing:
image

After fix - Memory use remains steady:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant