Search code examples
javalibgdx

LibGDX: What is the harm in keeping 1 SpriteBatch for everything and not disposing it?


I've been researching memory leaks, and I'm confused why SpriteBatch.dispose() is supposed to be called. I thought when you run spriteBatch.begin() and spriteBatch.end() that would be sufficient to prevent memory leaks and such.


Solution

  • No, begin() and end() are very different from the dispose method.

    According to the documentation,

    You are right about the behavior of the dispose method.

    Releases all resources of this object.

    begin() and end() are used to set the associated Batch for rendering, note that you can have multiple batches drawing. All draw calls on a batch must be done between the begin and the end methods. They have nothing to do with releasing memory, so that is why you need to do dispose() if you desire to do so.