libgdx The need to dipose assets on desktop

Using libgdx, desktop only game:

The docs are very much Android focused. So on Android its obvious, you gotta dispose everything.
My question is: Do you need to dispose things on desktop at all? Some of them? Never, always - which ones?

I do dispose but I also kinda assume that its not really necessary.

I was asking my self the same question sometimes…
I forgot disposing/unloading thigs verry often, but i never noticed any drawback cause of this (always only on desktop, while debugging, i never used libgdx on an android device till now…).
So i guess it is not really neccessary cause of performance, but you should do it, just because it is clan.
Just my thoughts…

My only slight concern would be VRAM/OpenGL cleanup. But even there not sure.
Since the Lwjgl probably cleans up by itself upon kill.

I am using the LWJGL backend.

well… other than the academic curiosity, I don’t see why not disposing. Your call to dispose is in the core project anyways, right?

In LibGDX the dispose methods are used for disposing things like Spritebatches, Textures, Audio, etc. If you’re making a Desktop version as well, you should still do cleanup.

Yeah, you should - but as @Cero asked: is it actually necessary or will everthing still be disposed if you don’t manually do it? :slight_smile:

I’m sure the OS will dispose all objects automatically on program exit, but that doesn’t mean that you shouldn’t dispose of unused objects. Otherwise you’ll get memory leaks and your program will continually use more nad more memory, which might crash the process or even the entire driver in some cases.

theagentd is right. Avoid keeping references on unused objects so that the garbage collector can do its job and keep in mind that the management of the native memory is up to you, i.e you can run out of native memory and then it’s probably your fault, look at the method BufferUtils.freeBuffer() or disposeUnsafeByteBuffer() depending on which version of LibGDX you use. This utility creates direct NIO buffers, those buffers are used under the hood when creating textures for example. I advise you to ensure that you know which native resources are really necessary when you start a level and to release them when you leave it.

Look at all instances of classes implementing the interface com.badlogic.gdx.utils.Disposable.

Of course, this advice concerns the developers who use the JogAmp backend too.

@theagentd Yeah I’m just taking about libgdx stuff using native memory or VRAM, not arbitrary objects in java

but yeah you guys are right
I kinda have it on my todo list since forever but never really done it: disposing routines for when something is no needed anymore… I guess the question is what and when. unloading everything not need on a given map during map change means I can never use the cache pretty much. Which is lame… hmm. Gotta have to calculate stuff.

Its a shame there isnt like a definitive reliable VRAM tool

Why do you still wonder when to do it? Do it when the player isn’t playing for example, typically when leaving a level, or somewhere in background when there is no such structure in a game.

Concerning “what”, look at the disposable objects.

Actually, there is a more complicated solution to use the cache even when unloading useless resources. You can compare the resources you used in the previous level with the resources you’re going to use in the next level in order to unload only the useless resources without wasting the interest of a cache. It’s doable, it’s just trickier to implement but it can decrease the time required to unload them. Imagine that there is an enemy that appears both in the level 2 and in the level 3, you won’t unload its meshes and its textures when you go from the former to the latter.

Yeah I would do that in any case. I mean you just compare the list of textures and sounds you have loaded to the one needed for the next map.
But I was unsure whether to unload everything I dont need each map change… well overall i guess my loadings and unloadings are rather quick so it should be fine; I really dont wanna have memory leaks.

However I have though about having a game without loading screens, with everything streaming… but yeah that seems like a huge pain. But also considering my game does feature cutscenes and jump to other locations occasionally, I probably shouldnt do that in the first place.

I guess it depends on the size of the game (or actually on the number and size of the ressources you use for it).
In a little game i started, i was planning to use 2 TextureAtlases, one for UI, one for the Game. When loading a level, i can simply load the GameAtlas, when leaving the level (and goind to MenuScreen), i can easily unload them. UIAtlas is alwas used, for example for the In-Game-Menu.
In some cases, at least for desktops, it might also be enough to load everything on startup and unload them when the game is closed.
Ofc, in bigger Games, with more and bigger Textures, Sounds, Models… thats no solution.

Yeah for casual games I did indeed just load everything in. But yes… little bigger now