简体   繁体   English

ArrayList.remove(index),垃圾收集是否可行?

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

When creating a game and you want to delete something from the screen that is in an ArrayList, lets say a bullet, is it viable to use arrayList.remove(index) to remove it from the game? 在创建游戏并且你想从ArrayList中的屏幕删除某些东西时,让我们说一个子弹,使用arrayList.remove(index)从游戏中删除它是否可行? or is it still using up memory when done this way? 或者这样做时是否还在耗尽内存?

If this is not the preferred way to do it, please point me in the right direction :) 如果这不是首选的方法,请指出我正确的方向:)

The answer is possibly. 答案是可能的。 Removing the bullet from the ArrayList will eliminate a reference to it. ArrayList删除项目符号将消除对它的引用。 If there are no other references, then the bullet object will be GCed eventually. 如果没有其他引用,那么子弹对象最终将被GC。

The screen of course will have to redraw itself without the bullet and that is a mostly separate issue. 屏幕当然必须在没有子弹的情况下重绘自己,这是一个大多是独立的问题。

If you remove an object from ArrayList, and that object doesn't have any other reference, then it would be 'eligible' for the garbage collector. 如果从ArrayList中删除一个对象并且对象没有任何其他引用,那么它将符合垃圾收集器的条件。

After that, you need not worry about removing it from the heap: JVM would do that through automatic garbage collector. 之后,您不必担心将其从堆中删除:JVM会通过自动垃圾收集器来实现。

For the question you asked ; 对于你提出问题 ;

or is it still using up memory when done this way? 或者这样做时是否还在耗尽内存?

The answer is; 答案是; YES , it will still be occupying memory unless the JVM calls for garbage collection and frees the memory. 是的 ,它仍将占用内存,除非JVM调用垃圾收集并释放内存。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM