MBZ*_*MBZ 1 java garbage-collection
我有一个关于GC如何在Java中工作的问题.请考虑以下代码:
class C1 {
ArrayList<int> myList = new ArrayList<int>();
public void setList(ArrayList<int> l) {
myList = l;
}
}
func(C1 C) {
ArrayList<int> l1 = new ArrayList<int>();
l1.add(1);
C.setList(l1);
}
main() {
C1 C = new C1();
func(C);
...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:GC func()
返回后是否发布'l1' ?
不,它没有,因为有一个根引用(堆栈变量C
),它有一个强引用(myList
),新的ArrayList
.之后main()
的回报,那么C1
和ArrayList
有收藏价值的,因为根参考消失.
归档时间: |
|
查看次数: |
89 次 |
最近记录: |