TIE*_*011 6 java arrays garbage-collection dereference
我已经对java垃圾收集器做了一些研究,并了解不再引用的对象将/应该由垃圾收集器处理.就对象数组而言,我知道将新对象分配给数组中的某个位置并不能正确释放先前分配的对象.
将数组中的对象设置null为另一个对象或将其设置为另一个对象使其符合垃圾回收的条件,假设没有对任何位置存储的同一对象的引用.
所以,如果你有
Object[] array = new Object[5];
Object object = new Object() // 1 reference
array[3] = object; // 2 references
array[1] = object; // 3 references
object = null; // 2 references
array[1] = null; // 1 references
array[3] = new Object(); // 0 references -> eligible for garbage collection
array = null; // now even the array is eligible for garbage collection
// all the objects stored are eligible too at this point if they're not
// referenced anywhere else
Run Code Online (Sandbox Code Playgroud)
垃圾收集很少会回收当地人的记忆,所以垃圾收集主要发生在函数范围之外.
| 归档时间: |
|
| 查看次数: |
14760 次 |
| 最近记录: |