Reputation: 97
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.
Upvotes: 2
Views: 344
Reputation: 6307
SpriteBatch.end()
Finishes off rendering. Enables depth writes, disables blending and texturing.
SpriteBatch.dispose()
actually releases all resources of this object, and prevents memory leak.
So after using SpriteBatch.end()
the item may be kept in memory depending on other aspects of your code, especially if you want to re-use it again later. So dispose should always be called when you are completly finished with the SpriteBatch, else there is no guarantee that the memory will be freed.
Upvotes: 2
Reputation: 20140
Disposable resources need to be disposed but where ?
If you're creating your SpriteBatch
in create()
method of ApplicationListener
and using it all over the game on different screen, dispose them ApplicationListener's dispose()
method.
Else if you're creating in Screen's show()
method then dispose in Screen's dispose()
method but dispose method not called by ApplicationListener or Framework, so you need to call dispose method by yourself so call dispose method from hide()
method of Screen.
begin()
and end()
method is use to achieve the Purpose of SpriteBatch
:
To batch multiple sprites into a single draw call in order to minimize the associated performance penalty of each draw call.
Upvotes: 1
Reputation: 1199
You definitely do not want to dispose it every frame or after every end
. It will not build up. Once you are finished using the batch, you want to dispose it (which is most likely when closing the application or changing Screen
)
Upvotes: 3
Reputation: 1199
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.
Upvotes: 4