Memory leak in ModelBatch.render()
See original GitHub issueWhile profiling my Android application I have noticed that every frame the number of allocated objects is increasing (see the white line on the screenshot below).
Issue details
Preliminary investigation has shown that every call of ModelBatch.render()
creates an instance of ArrayList
iterator.
As I understand, while there is a forEach()
call, Iterable<T>
create the iterator on every call, every frame.
for (final RenderableProvider renderableProvider : renderableProviders)
render(renderableProvider, environment);
I know there is a flag Collections.allocateIterators
which could be a reason for that (by default it is “false”, which is good). Even when I have set it to “false” explicitly, it did not help.
Reproduction steps/code
My code which call the render is quite trivial:
modelBatch.begin(camera)
modelBatch.render(modelList1, environment) // modelList1 - ArrayList<ModelInstance>
modelBatch.render(modelList2, environment) // modelList2 - ArrayList<ModelInstance>
modelBatch.end()
Version of LibGDX and/or relevant dependencies
gdxVersion = ‘1.9.10’ roboVMVersion = ‘2.3.8’ box2DLightsVersion = ‘1.4’ ashleyVersion = ‘1.7.0’ aiVersion = ‘1.8.0’
KotlinVersion = “1.3.72” Java version = “1.8.0_251”
Stacktrace
iterator:806, ArrayList (java.util)
render:272, ModelBatch (com.badlogic.gdx.graphics.g3d)
render:76, MySceneRenderer (com.mysupergame.game.ui)
...
Please select the affected platforms
I have experienced that for Android platform, and did not check for others, assuming it is not related to the platform.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
The memory issue you describe is not a memory leak nor the problem is in the
render()
implementation. Use anIterable
implementation that doesn’t allocate by makingmodelList
acom.badlogic.gdx.utils.Array
instead of ajava.uitil.ArrayList
.@obigu , thank you for the advice using the
com.badlogic.gdx.utils.Array
instead ofjava.uitil.ArrayList
. It has helped. Now I would say it is an imperfection ofArrayList
implementation. Anyway, it is not related torender()
method now. I close the issue.