Reputation: 43893
In android, I make use of hashmaps and sparsearrays and sparseintarrays. In functions I use them sometimes are temporary objects. Before the function ends, it is a good practice to clear the map's data or not?
Thanks
Upvotes: 1
Views: 100
Reputation: 54801
No.
It is unnecessary to do so. As the hashmap is local to the function, the hashmap will be collected by the garbage collector and it is of no difference if it is empty or not. The contents of the hashmap will also be collected if it has no other reference. It will not be collected any faster in or out of the map.
Therefore clearing it simply wastes your time and clutters your code.
If however you are worried about memory, it must be some pretty large objects. If it is say a Bitmap
, you can call bmp.recycle()
to help garbage collection out.
Upvotes: 3
Reputation: 46239
In android, I make use of hashmaps and sparsearrays and sparseintarrays. In functions I use them sometimes are temporary objects. Before the function ends, it is a good practice to clear the map's data or not?
No, that accomplishes nothing.
I thought, if you clear it, then you get back the memory right away instead of waiting for garbage collection. So doesn't that make it work it?
Clearing the collection only gets rid of the references to the items in the collection, so you still have to wait for garbage collection to get the memory back.
Upvotes: 5