简体   繁体   中英

When to dispose screens in LibGDX?

The question is a bit of a throw off, as I don't mean disposing the screen itself. What I am using is an enum and switch statement to switch screens, rather than the Screen and Game classes. What I am really asking here is, when I switch from one game state to the other, am I supposed to dispose all of my disposables before hand? Or do I just keep all of them and not worry about it, despite not rendering them anymore since I'm rendering a separate screen? I'd find it annoying to have to dispose all the resources on my screen every single time I want to switch to another one, so I'm wondering if it's truly necessary.

It's your choice and type of your game(Is your game having many resources).

If having lots of resources then it's better to dispose resource of one screen then load resource of another screen in memory and after that use that resource.

In this scenario show loading screen and load resource asynchronously.

If don't having lots of resources, dispose resource only when you exit from your game. Inherited Game's dispose() is best place for dispose your game resource in this scenario and call screen dispose from here. so that screen specific resource can disposed.

Quick rules can be:

If you loading something (Texture, TextureAtlas, Music, Sound etc.) at application start and you do not have problem with memory heap - dispose it at application dispose method.

But if you loading something special for your screen at screen start you should dispose it in screen dispose method.

If you do not dispose resource for ex. Texture it will be still in memory even if you dont render it. When you loading some texture inside screen without dispose it inside screen dispose method, it maybe to cause a memory leak because you load same texture second time when you start your screen again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM