• Post Reply Bookmark Topic Watch Topic
  • New Topic

garbage collection question  RSS feed

 
anagha desai
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
If I declare a List (collection object ) in a method ( local variable ) and then I populate it with a couple of other objects .

Do some business logic and then return a boolean value .

Essentially the Collection is not returned .

In such a case ( since the collection is populated with other objects )
would it be a good idea to explicitly clear the collection of the other objects

( My understanding is that thios will not be necessary since the collection is getting deferenced after scope of method is completed )

However would the garbage collector perform more efficciently if I were to clear the collection or would it make no difference ?

Regards,
-anagha
 
akhilesh pandey
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you know that we cannot force garbage collection in java but we can only recommend it.So I think it would not make much difference.
 
arulk pillai
Author
Ranch Hand
Posts: 3417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It won't hurt to do

myCollection.clear();
myCollection = null;
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by arulk pillai:
It won't hurt to do


It won't break your program, but it is pointless.

If you create a collection in a method, then return from the method, leaving no references to the collection, then the collection will be GC'd, at some later point. Clearing the collection will not make the GC run faster or happen sooner.

By adding such unnecessary code, you obscure the true meaning of the code, making it harder to debug and to maintain. Therefore, I recommend you do not add such code to your methods.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This can be helpful if you want to use the same collection more than one time with different values in it.
 
anagha desai
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your inputs .
My question was not regarding clearing a collection for reuse but was related to GC.

Thank you all for your thoughts.

Regards,
-anagha
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishal Pandya:

This can be helpful if you want to use the same collection more than one time with different values in it.


... but that is only very rarely worthwhile. Allocating a new collection is clearer, safer (you're sure no-one else is using your new collection) and barely any slower.

In fact, clear() typically does not reduce the size of the internal arrays used in the collection. So, if your old use of the collection had a huge number of entries, but your new use of it only has a few entries, you will negatively impact performance, by forcing your program to hold on to a huge array, when a new collection would have used a suitably small array.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
... but that is only very rarely worthwhile.

What is the use of clear method? By using(clearing the Map) it you can reuse the same collection.Isn't it?

I use the same Map more than one time in different queries.
Like I set the query parameters in Map(Parameter Name,Parameter Value) and use the Map in query.Again if i need to execute one more query then i will clear this Map and set new parameters into it and reuse it.

Is this good coding style? Any suggestion.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishal Pandya:

What is the use of clear method? By using(clearing the Map) it you can reuse the same collection.Isn't it?


You can use it that way, but it isn't often a good idea. As already mentioned, reusing collections is rarely wise.

Originally posted by Vishal Pandya:
I use the same Map more than one time in different queries.
Like I set the query parameters in Map(Parameter Name,Parameter Value) and use the Map in query.Again if i need to execute one more query then i will clear this Map and set new parameters into it and reuse it.

Is this good coding style? Any suggestion.


No. The re-use is unlikely to save significant time or memory, and might actually cost time or memory. Stick with the simple, obvious option: make a new collection every time.

(Of course, like most "rules" in programming, it is not absolute. Occasionally, re-use saves significant time or memory, or makes code simpler. But this is not common, and re-use should not be your default choice).
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any use of clear() method?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!