Is it better to create a new HashSet or reuse after calling hashSet.clear ()

I want to process some new data in a HashSet without any old data or an old HashSet object. The old HashSet is not mentioned elsewhere.

Is it better to just do hashset = new HashSet<String>() and let the JVM free the memory of the old HashSet or should I call hashSet.clear() and reuse the same HashSet?

According to openJDK, hashSet.clear ():

 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 over all entries, will it be a lot of time when the hashSet is large? Which one is recommended in this case, the constructor method or the clear () method?

+6
source share
7 answers

HashSet cleaning is not required - just delete all references to the HashSet (what happens if you write hashset = new HashSet<>(); if the old HashSet is not mentioned anywhere), and the garbage collector will do the rest.

Also note that if you need a new HashSet of the same size, calling hashset.clear() takes longer than just creating a new HashSet with the appropriate capacity

+4
source

Just don’t refer to the HashSet anymore and let the garbage collector do the work.

Clearing a HashSet before dereferencing does not free up memory faster.

+6
source

I think there is a misconception here. You do not free memory in java manually. Even if you call System.gc(); That does not guarantee that the GC will be launched at this moment.

As for your question: I think you should use your links in the smallest possible area, and when the code goes out of the scope of your Set , the link to it is simply dropped and GC collects it.

clear(); I think that it is recommended to use it if you have additional work with Set : for example, if you processed the data in it and you are preparing to insert some other data into it.

In practice, I almost never see explicit mySet = null; (this is what you should use if you want to explicitly dereference, because mySet = new HashSet<>() does not make sense), because it is much simpler and less cumbersome to just use the corresponding area.

+3
source

Unable to free memory in java. All you can do is let the VM know that everything is in order to free it if and when it wants. This will usually happen when everything goes beyond. In general, everything is in order not to worry about it. If you are profiling an application and you see a memory leak, you can try to find it.

Definitely do not create a new HashSet. clear () will remove all elements from the HashSet, but it still will not be collected until it becomes inaccessible.

+2
source

If you do not have references to your objects, the garbage collector automatically deletes them. You do not need to do this manually.

0
source

It depends, if you have very small cards, then in this case cleaning will be better than spawning objects.

And if you have many items on your cards, then you should not even clean it. As if you see an implementation of the clear() method, it iterates over all the elements. In this case, the creation of a new facility will be slightly cheaper.

But, as a rule, it is recommended that you collect the garbage collector to clean things up, and you continue to create new objects while the left link is cleared by the garbage collector.

But, as a rule, you need to try to limit the number of objects created. As I said, it depends.

0
source

Memory management is performed by the JVM itself. Thus, even if you make the memory free by hand or not, it will automatically collect garbage.

The garbage collector reduces the coding effort associated with memory management.

0
source

Source: https://habr.com/ru/post/952732/


All Articles