0

Do I have to .clear() HashSet and HashMap everytime, when variable is no longer used?

I have a lot of temporary variables in methods in my application that are HashSets and HashMaps. Do I have to clear them everytime when, for example, method is done? Will GC remove it automaticly if object will not be longer used by something?

I am asking because a lot of people point that I have to clear data in any hashmap if program is done and data stored in hashmap will not longer be used.

Albert451
  • 79
  • 8
  • 1
    Are you asking because of [this question](https://stackoverflow.com/questions/28407307/how-to-clear-objects-hashmap-to-be-garbage-collected-java)? If so, I think it depends on the use case. If the `HashSet`s and `HashMap`s are so large they take up a large amount of memory that you need to keep clearing, then yes. But the answer is probably no, especially if the variables are small. – Jonathan Lam Apr 27 '18 at 14:28

3 Answers3

1

When the hashmap goes out of scope, it would be automatically garbage collected, so you shouldnt need to call clear if all you are doing is letting to getting removed.

Clear is good for if you want to re-use the map but want to empty it of all data.

Gibbon
  • 2,633
  • 1
  • 13
  • 19
0

GC Will clear it for you when there's a lack of memory .. but it's better to set your references that you won't use anymore to null

0

HashMap and HashSet are objects in java. So when an object is no longer referred and is out of reach it would become eligible for Garbage Collection.

However, if you think there is a memory leakage then I recommend to explicitly set all the Object references of the related HashMap and HashSet to null immediately after you don't need them.

You can refer to memory leakage here:

https://dzone.com/articles/memory-leak-andjava-code

If you are not sure about memory leakage, for the safe side you can set the references of the objects to null.

Shubham Kadlag
  • 2,248
  • 1
  • 13
  • 32
  • I have read about that the memory leak may occour if Object will not have implemented hashCode() or equal() method before adding to HashSet (https://stackify.com/memory-leaks-java/). Is it true? – Albert451 Apr 27 '18 at 15:00
  • Ideally all bean / POJO style classes should override hashcode() and equals(). In this case its useful since the actual duplicate objects are considered unique. For eg. class Student{ int roll_no;} Here if you don't override hashcode and equals method object with same roll no would be considered different. So it is highly recommended to override them. If you are using an IDE like Eclipse there is an option to generate hashcode() and equals() method. – Shubham Kadlag Apr 27 '18 at 19:34
  • Thanks for your reply. So - it is useful only if the objects from our point are same (like String without hashcode), but are unique in java memory? – Albert451 Apr 28 '18 at 15:55
  • Yes, because hashmap, hashset use hashcode and equals method to determine whether the objects you are trying to insert in it is already present or not. – Shubham Kadlag May 02 '18 at 14:48