Java source Hashtable

1

I don't known how to search this question, so I asked a question.
Java version 1.7.0_80_x86
The remove method in java.util.Hashtable;
I see that the value attribute of node e is set to null;
However, e.next is not set to null;
So if e.next is not null, will node e not be reclaimed by gc?
Method source code:

/**
 * Removes the key (and its corresponding value) from this
 * hashtable. This method does nothing if the key is not in the hashtable.
 *
 * @param   key   the key that needs to be removed
 * @return  the value to which the key had been mapped in this hashtable,
 *          or <code>null</code> if the key did not have a mapping
 * @throws  NullPointerException  if the key is <code>null</code>
 */
public synchronized V remove(Object key) {
    Entry tab[] = table;
    int hash = hash(key);
    int index = (hash & 0x7FFFFFFF) % tab.length;
    for (Entry<K,V> e = tab[index], prev = null ; e != null ; prev = e, e = e.next) {
        if ((e.hash == hash) && e.key.equals(key)) {
            modCount++;
            if (prev != null) {
                prev.next = e.next;
            } else {
                tab[index] = e.next;
            }
            count--;
            V oldValue = e.value;
            e.value = null;
            return oldValue;
        }
    }
    return null;
}
java
garbage-collection
hashtable
asked on Stack Overflow Jul 16, 2018 by Michael • edited Jul 16, 2018 by Radiodef

1 Answer

0

Every object tree must have one or more root objects. As long as application can reach these roots they are considered as live object if application cant reach these roots then they are collected by garbage collector. So answer to your question is yes "e" will be garbage collected by GC. But "e.next" may or may not be garbage collected by GC. If their is a reference to "e.next" (other than "e") then it will not else it will garbage collected by GC.

answered on Stack Overflow Jul 16, 2018 by Keaz

User contributions licensed under CC BY-SA 3.0