hajimehoshi / ebiten

Ebitengine - A dead simple 2D game engine for Go
https://ebitengine.org
Apache License 2.0
10.8k stars 651 forks source link

Font cache causing memory leak #3110

Closed t-geindre closed 3 hours ago

t-geindre commented 4 hours ago

Ebitengine Version

master

Operating System

Go Version (go version)

1.23

What steps will reproduce the problem?

When calling

text.DrawWithOptions(screen, "some text", myFace, opts)

Font is cached here I'm working on an application using a lot of various fonts and I can't keep all of them loaded.

What is the expected result?

Memory usage remains stable.

What happens instead?

Memory is leaking.

Anything else you feel useful to add?

I couldn't find a way to clear this cache. Here is what I found when debugging the memory usage: image

t-geindre commented 3 hours ago

For now, I'm solving the issue like this:

//go:linkname FontCache github.com/hajimehoshi/ebiten/v2/text.faceWithCacheCache
var FontCache map[font.Face]any

func switchFont() {
     // loading new one, removing old one, and then clear ebiten cache
     delete(FontCache, oldFont)
}
hajimehoshi commented 3 hours ago

https://github.com/hajimehoshi/ebiten/issues/498