ArrayList.remove (index), is it viable for garbage collection?

When creating a game, and you want to remove something from the screen that is in the ArrayList, say, a bullet, is it possible to use arrayList.remove(index) to remove it from the game? or is he still using memory when this is done?

If this is not the best way to do this, please point me in the right direction :)

+4
source share
2 answers

Perhaps the answer. Removing a bullet from an ArrayList will remove the reference to it. If there are no other links, then the bullet object will be GCed in the end.

The screen, of course, will have to be redrawn without a bullet, and this is basically a separate problem.

+4
source

If you remove the object from the ArrayList, and that the object has no other reference , then it will be "acceptable" to the garbage collector.

After that, you don’t have to worry about removing it from the heap: the JVM will do this through an automatic garbage collector.

For the question you asked ;

or is he still using memory when this is done?

Answer: YES , it will still take up memory, if only the JVM requires garbage collection and frees up memory.

Hope this helps.

+3
source

Source: https://habr.com/ru/post/1494158/


All Articles