Re: TextRenderer and memory issues

Posted by yaqiang on
URL: https://forum.jogamp.org/TextRenderer-and-memory-issues-tp4041129p4041955.html

I also experienced TextRenderer memory leak issues when rendering string using new TextRenderer objects. My solution is using only one TextRenderer object and update it when new font needed.

Update TextRenderer in the display function and dispose and null it at the end of the function (https://github.com/meteoinfo/MeteoInfo/blob/master/meteoinfo-chart/src/main/java/org/meteoinfo/chart/jogl/GLPlot.java#L1224-L1320).

With a new font, following updateTextRender method is used.

The TextRenderer update function (https://github.com/meteoinfo/MeteoInfo/blob/master/meteoinfo-chart/src/main/java/org/meteoinfo/chart/jogl/GLPlot.java#L2267-L2285):

    void updateTextRender(Font font) {
        boolean newTR = false;
        if (this.textRenderer == null) {
            newTR = true;
        } else {
            if (!this.textRenderer.getFont().equals(font)) {
                //this.textRenderer.dispose();
                newTR = true;
            }
        }
        if (newTR) {
            if (this.dpiScale == 1) {
                textRenderer = new TextRenderer(font, true, true);
            } else {
                textRenderer = new TextRenderer(new Font(font.getFontName(), font.getStyle(),
                        (int) (font.getSize() * this.dpiScale)), true, true);
            }
        }
    }

It works fine at present. Any better solution is welcome.
www.meteothink.org