LibGdx - Dispose Sprite or Texture in Sprite is mandatory?

我的梦境 提交于 2019-12-11 09:03:48

问题


I am loading my sprite with the texture from my assets like I have mentioned below.

gameBoardSprite = new Sprite( new Texture(Gdx.files.internal("data/gameboard.png")));  

Do I need to have the below dispose in my code ? Is it mandatory ?

gameBoardSprite.getTexture().dispose();

回答1:


Yes, dispose must be called on any Disposable object before you lose your reference to it or there will be a memory leak. The VM won't automatically dispose of the native memory used by OpenGL objects like Textures so you must manually do it before you drop your reference and let the GC claim the "non-native" memory claimed by the object itself.

If you are going to use a Texture for the lifetime of your app, you might not be planning to ever need to dispose it, but on Android you still have to in the Game's dispose method because there are cases where Android will shut down your Activity but not the entire Application so when the user reopens your game, all the previous textures are leaked.




回答2:


Well technically you don't have to dispose it, BUT not disposing it WILL impact your performance.

On android Dalvik will kick in and decide whether or not to free up some of your memory. Thus causing frame loss.

On your desktop, memory leaks may happen.

Read here for more information



来源:https://stackoverflow.com/questions/29574504/libgdx-dispose-sprite-or-texture-in-sprite-is-mandatory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!