0

I developing my game using libgdx and libktx. my main game class is:

class MyGame : KtxGame<KtxScreen>() {
val assets = AssetManager()

override fun create() {
    addScreen(LoadingScreen(this))
    setScreen<LoadingScreen>()
    super.create()
}

override fun dispose() {
    assets.dispose()
    super.dispose()
}
}

I want to restart my game screen after the game is over. for that, I remove the current game screen and add another one.

in my GameScreen.kt:

        myGame.removeScreen<GameScreen>()
        dispose()


        myGame.addScreen(GameScreen(myGame))
        myGame.setScreen<GameScreen>()

and dispose method is:

override fun dispose() {
        stage.dispose()
        super.dispose()
    }

I added some actors to this stage.

using this methods I monitoring memory usage:

val javaHeap = Gdx.app.getJavaHeap()/1000000
val nativeHeap = Gdx.app.getNativeHeap()/1000000

but after each time add a new screen (and remove older) about 30 Mb added to ram in Android. in Desktop some times adds nothing some times about 2Mb. for android I tested and ram allocated more than 1Gb after multiple remove and adding another screen.

Hadi Ahmadi
  • 1,924
  • 2
  • 17
  • 38
  • It is difficult to tell without seeing the rest of the code. Is it possible that you're creating other memory-intensive objects and leave references to them somewhere within the app? Providing a minimal reproducible example would help answering this question or even finding an issue in your original application. My suggestion would be to dispose of the screens individually and stop reloading the assets each time to restart the game. – Czyzby Mar 24 '21 at 13:42
  • @Czyzby thanks for your response. I test it again and I realize if ram goes more than 2Gb, then free up. it seems GC does not work until ram is needed. – Hadi Ahmadi Mar 26 '21 at 16:54
  • Yeah, that's also a possibility. Memory might not be freed immediately. You can answer your own question and accept the answer, since you've solved the issue. – Czyzby Mar 27 '21 at 08:25

0 Answers0