问题描述
示例代码:
Hashtable h0 = new Hashtable();
h0.add(0,10);
Hashtable h1 = new Hashtable();
h1.add(0,h0);
h1.Remove(0);
问题:
我是否需要获取h0
,删除h0
中的项目,然后从h1
中删除h0
?或者只是删除h0
?
Do I need to get the h0
, remove the items in h0
, then remove the h0
from h1
? Or just remove the h0
?
推荐答案
通过从h1中删除h0,您告诉垃圾收集器嘿,h0不再被任何东西引用,因此您可以对其进行垃圾收集".一旦h0被垃圾回收,这意味着也不会引用带框的整数(10),也使其成为垃圾回收的候选对象.
By removing h0 from h1, you tell the garbage collector "hey, h0 is no longer referenced by anything, so you can garbage collect it". Once h0 is garbage collected, that means the boxed integer (10) is also not referenced, making it a candidate for garbage collection as well.
垃圾回收可以归纳如下:如果一个对象不再被另一个对象引用,则将对其进行垃圾回收(又名从内存中释放).如果存在某种循环引用(例如,变量A引用变量B,然后引用变量A),则只要主线程可以访问的任何对象都没有引用这些对象,这些对象仍将被垃圾回收.
Garbage collection can be summarized as follows: if an object is no longer referenced by another object, it will be garbage collected (aka, released from memory). If there is some sort of circular referencing (such as variable A referencing variable B which then references variable A), then those objects will still be garbage collected so long as they aren''t referenced by anything that the main threads can access.
这篇关于关于哈希表删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!