# Android Game - Screen Flickers When an Entity Removed From The List

I am using Canvas in my android game. When I remove a no longer displayed entity in my entity list, all other entities are flickering for a brief time. When it's not removed, there is no such problem. But since I am not a big fan of memory leaks, that's not an option.

The canvas rendering system is already double-buffered by design and I have utterly no idea how to fix such a problem. I have thought maybe it is because the list is sorting itself after the item removal and tried changing it to a Set, but that didn't work either.

Does anyone have any idea why this might be happening and how to fix it?

Structure of the code:

private val gameObjects: List<GameObject> = mutableListOf()

fun update(deltaTime: Long)
{
gameObjects.forEach {
it.update(deltaTime)
}

fun render(canvas: Canvas)
{
gameObjects.forEach {
when (getVisibilityStatus(it.virtualY))
{
VisibilityStatus.VISIBLE -> it.render(canvas, virtualToPhysicalY(it.virtualY))

VisibilityStatus.BELOW_SCREEN ->
{
if (virtualToPhysicalY(it.virtualY) > screenSizePairXY.second)
gameObjects.remove(it)

}
}
}


## 1 Answer

Apparently removing an item from a list that you are iterating isn't such a good idea. Separating the cleanup/removal process from the render for each loop fixed the issue.

      private val gameObjects: List<GameObject> = mutableListOf()
private val gameObjectsToRemove: List<GameObject> = mutableListOf()

/**
* Removes the invisible objects (if any) to save memory
*/
private fun cleanUp()
{
gameObjectsToRemove.forEach {
gameObjects.remove(it)
}
gameObjectsToRemove.clear()
}

fun update(deltaTime: Long)
{
cleanUp()
gameObjects.forEach {
it.update(deltaTime)
}

fun render(canvas: Canvas)
{
gameObjects.forEach {
when (getVisibilityStatus(it.virtualY))
{
VisibilityStatus.VISIBLE -> it.render(canvas, virtualToPhysicalY(it.virtualY))

VisibilityStatus.BELOW_SCREEN ->
{
if (virtualToPhysicalY(it.virtualY) > screenSizePairXY.second)
gameObjectsToRemove.add(it)

}
}
}

• You might want to use removeAll instead of calling remove in a loop. – DMGregory Sep 1 at 13:13
• Yeah sure why not, thanks – DeveloperKurt Sep 1 at 14:52