Is it better to free memory manually in java code?
If there is a HashSet<String> hashSet which I don't need any longer.
Is it better to simply do hashset = new HashSet<String>() and let JVM to
free the memory or should I use hashSet.clear() instead?
According to openJDK, hashSet.clear() is like this:
public void clear() {
map.clear();
}
and map.clear() :
public void clear() {
modCount++;
Entry[] tab = table;
for (int i = 0; i < tab.length; i++)
tab[i] = null;
size = 0;
}
Since Map.clear() iterates all the entries, will it be time consuming when
the hashSet is large? Which one is recommended, the constructor or the
clear() method?
No comments:
Post a Comment