3

I am using a BitmapFont as follows:

create(){
    font = new BitmapFont(getFileResource("50.fnt"), getFileResource("50.png"),
            false);
}

dispose(){
font.dispose();
 }

I have several screens that load and unload fonts of different sizes. As time goes by, the memory fills up.

After spending a long time looking for a memory leak, I find this BitmapFont class is leaking. I think it is leaking on native memory because the leak is not seen using Memory Analyzer.

I am following the procedure to clean up the memory according to the current documentation. But this is not enough. What else should I do to ensure the BitmapFont lets go of its memory ?

jeremyvillalobos
  • 1,795
  • 2
  • 19
  • 39

2 Answers2

2

This might be a bug. Here you can see your constructor. And here a very similar one. The difference is that the 2nd sets the ownsTexture flag. Only if this flag is set, the texture gets disposed.

I will create an issue/PR to fix this problem or at least make it behave the same way, or add a JavaDoc warning.

For now you can solve this problem by doing bitmapFont.setOwnsTexture(true) yourself.

noone
  • 19,520
  • 5
  • 61
  • 76
  • I've just noticed that I was not completely right. The one constructor forwards to the other and both set the flag. However then two other constructors get invoked and the 4th one sets the flag back to `false`. So if you want the font to "own" (and dispose) the created Texture, you need to set the flag for now yourself. – noone Jun 24 '14 at 08:42
  • No, I think I'm completely *wrong*. The flag is set to `true` in any case when you manually pass in a FileHandle. So there should not be a memory leak actually. – noone Jun 24 '14 at 08:53
0

The error was that the libgdx code is long running, so I could not wait for dispose to be called.

So I was calling dispose on the elements that were no longer needed on the screen from a thread other than the GLThread (on Android)

Libgdx ignores dispose() when not coming from the GLThread().

Adding the clean up code on render such that it runs when outdated components pile up fixed the problem.

jeremyvillalobos
  • 1,795
  • 2
  • 19
  • 39