High RAM usage when displaying long texts
Author: Sesu8642Created Nov 10, 2024Updated Sep 28, 2026
Issue details
In my game, I display the license texts of the free / open source dependencies. To do so, I currently use a label inside a scroll pane. The size of the license texts is quite large, around 3.7 MB. As soon as i show the screen, the RAM usage increases by several 100 MB and causes an OutOfMemoryError on Android devices. I also tried using a TextArea which has the same issue but to a lesser extend.
Reproduction steps/code
In this minimal example, the ram usage increases from around 100 MB to 1.8GB despite the text content itself only being 10 MB.
public class Main extends ApplicationAdapter {
private Stage stage;
@Override
public void create() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 1000000; i++) {
stringBuilder.append("aaaaaaaaa\n");
}
String text = stringBuilder.toString();
System.out.println("string size: " + text.getBytes().length + " bytes"); // size is 10 MB
stage = new Stage(new ScreenViewport());
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("FreeSans.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 16;
BitmapFont font16 = generator.generateFont(parameter);
generator.dispose();
LabelStyle labelStyle = new LabelStyle(font16, Color.WHITE);
Label label = new Label(text, labelStyle);
stage.addActor(label);
}
@Override
public void render() {
ScreenUtils.clear(0.15f, 0.15f, 0.2f, 1f);
float delta = Gdx.graphics.getDeltaTime();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
@Override
public void dispose() {
stage.dispose();
}
}Version of libGDX and/or relevant dependencies
tested with libGDX 1.13.0 and 1.12.1
Stacktrace
11-10 20:59:03.691 32570 32607 E AndroidRuntime: java.lang.OutOfMemoryError: Failed to allocate a 290269856 byte allocation with 100663296 free bytes and 118MB until OOM, target footprint 244968048, growth limit 268435456
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.graphics.g2d.BitmapFontCache.requirePageGlyphs(BitmapFontCache.java:344)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.graphics.g2d.BitmapFontCache.requireGlyphs(BitmapFontCache.java:318)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.graphics.g2d.BitmapFontCache.addToCache(BitmapFontCache.java:382)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.graphics.g2d.BitmapFontCache.addText(BitmapFontCache.java:531)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.graphics.g2d.BitmapFontCache.setText(BitmapFontCache.java:486)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.Label.layout(Label.java:223)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.Widget.validate(Widget.java:88)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.Table.layout(Table.java:1165)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.validate(WidgetGroup.java:104)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.Container.layout(Container.java:153)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.validate(WidgetGroup.java:104)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.Stack.layout(Stack.java:107)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.validate(WidgetGroup.java:104)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.layout(ScrollPane.java:498)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.validate(WidgetGroup.java:104)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.Table.layout(Table.java:1165)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.validate(WidgetGroup.java:104)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.ui.Table.draw(Table.java:108)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:111)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:58)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:128)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at de.sesu8642.feudaltactics.menu.common.ui.GameScreen.render(GameScreen.java:42)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.Game.render(Game.java:48)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:505)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1573)
11-10 20:59:03.691 32570 32607 E AndroidRuntime: at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1272)Please select the affected platforms
- Android
- iOS
- HTML/GWT
- Windows
- Linux
- macOS
(others are likely affected too)
Source: libgdx/libgdx