3

我有一堆对象ArrayList,如果我打电话ArrayList.remove(object)我需要做任何其他事情来从内存中删除对象吗?我正在以相当快的速度从这个列表中添加和删除对象,所以如果它没有从内存中删除,它将开始占用空间并开始减慢游戏速度。

4

5 回答 5

5

-当您调用时ArrayList.remove(object),您只需从List Not from the Memory中删除对象。

-这将取决于Garbage Collector决定何时从堆中删除对象,在正常情况下,它的对象已准备好进行垃圾回收,因为它不再引用它。

-有一个经典示例说明为什么不应使用StringJava 中的对象来存储密码,而应使用该对象。char[]

看这个链接...

为什么 char[] 优于 String 的密码?

于 2012-12-24T02:38:28.703 回答
1

if you chew through the heap quick enough, you can nudge the gc along with some jvm args ... we have an app that handles billions of operations a day and have tuned it heavily with the ergonomic gc settings I'd encourage you to play with the adaptive size policy, and the max pause setting primarily. Run the program with a profiler exercising the arraylist as you normally would for a while (several minutes) and see how the ambient state of the various heap generations looks. You may end up having to also tweak the memory allocations to generations.

于 2012-12-24T04:04:49.477 回答
1

Java 进行自动垃圾收集。所以一旦一个对象不再被引用,它就可以被删除,但这并不意味着它会被删除。垃圾收集是自动的,您可以通过调用 System.gc() 来要求它完成,但这只是运行它的建议。

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#gc%28%29

于 2012-12-24T02:37:40.523 回答
1

不,您不必做任何其他事情,只要那是引用该对象的唯一地方。欢迎来到垃圾收集语言的乐趣!当 Java 决定需要回收一些内存时,它会清理旧的、未引用的对象。

于 2012-12-24T02:37:43.790 回答
0

ArrayList.remove 从数组中删除对象,然后如果该对象没有被任何其他对象引用,GC 将删除该对象。

于 2012-12-24T02:38:58.097 回答