java - libgdx game start freezing after some tim -


my game start freezing after time (take ram), want try draw 3( later more) string using loop method. code this:

    public class simple implements applicationlistener {         private orthographiccamera camera;         private spritebatch batch;         private bitmapfont font;         private glyphlayout layout;         string a1 = "aa";         string a2 = "bb";         string a3 = "cc";         int = 0;          @override         public void create() {             camera = new orthographiccamera();             camera.settoortho(false, 800, 480);             batch = new spritebatch();         }          @override         public void render() {             gdx.gl.glclearcolor(0, 0, 0.2f, 1);             gdx.gl.glclear(gl20.gl_color_buffer_bit);             camera.update();             batch.setprojectionmatrix(camera.combined);             batch.begin();             (int = 1; < 4; i++) {                 layout = new glyphlayout();                 font = new bitmapfont();                 layout.settext(font, "a" + i);                 font.draw(batch, layout, 200 + (15 * i), 200);             }             batch.end();         }     } 

to visualize epicpandaforce mentioned in comments:

public class simple implements applicationlistener {     private orthographiccamera camera;     private spritebatch batch;     private bitmapfont font;     private glyphlayout layout;     string a1 = "aa";     string a2 = "bb";     string a3 = "cc";     int = 0;      @override     public void create() {         camera = new orthographiccamera();         camera.settoortho(false, 800, 480);         batch = new spritebatch();         //initialize fields in create()                    layout = new glyphlayout();         font = new bitmapfont();      }      @override     public void render() {         gdx.gl.glclearcolor(0, 0, 0.2f, 1);         gdx.gl.glclear(gl20.gl_color_buffer_bit);         camera.update();         batch.setprojectionmatrix(camera.combined);         batch.begin();         (int = 1; < 4; i++) {             //use them do.             layout.settext(font, "a" + i);             font.draw(batch, layout, 200 + (15 * i), 200);         }         batch.end();     } } 

i'm not sure if going work. not specifying bitmapfont anywhere when draw using layout nullpointerexception. in create method want initialize font font = new bitmapfont(gdx.files.internal("path bitmapfont"));.

using new expensive operation. update() method being called every frame , creating new objects each time being called. on top of bitmapfont() not small object. , each time create new object font , layout previous object holded needs collected garbage collector. basic rule never use new keyword in update() method rather change and/or use did now. visualize in simple way:

object o; // <-- simple container new object(); // <-- object stored in memory in it's glory  object o = new object(); // <-- container o pointing memory address of new object()  object o = new object(); // container 0 pointing different object in memory //but old 1 still chilling @ it's own address , needs collected 

Comments