Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it nesscessery to handle Garbage collection

 
Ramesh kumaar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

Is it necessary to handle Garbage collection In our project.

regards,
-rameshkumar.
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you have to do is set the reference to null whenever possible. You don't even have to call System.gc().
 
Ramesh kumaar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prasad,
You mean when i come to know that there is no more use of a purticular object than i should kill the object by making object=null, am i right correct me if iam wrong.
thanks & regards,
-rameshkumar
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. That is correct.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless it is a method local object that will become null once the method is finished running.
There are some who will say that even in that case it is better to set all objects to null, irregardless of when they will be out of scope.
I disagree, I am highly in the pretty looking, keep the method it to a few lines kind of guy, I find it is much easier to read. But others have their opinion too, and I think that is fine, I am not trying to get into any religious programming war here. So this will be my last post on this subject here.
Mark
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

All you have to do is set the reference to null whenever possible.

If you take it too literally, half of your code will set objects to null. This is way too ugly. Remember, Java garbage collector has its own facilities to know when your objects can be garbage collected. Even if you put your "set to null" everywhere, it is very likely that garbage collector will ignore it, -- it might release all memory at once when your program is finished.
I would say a reasonable rule is "Set the reference to null whenever
neccessary, not whenever possible." This neccessaty might come up in a few situations where there are memory leaks.
Eugene Kononov.
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI. I don't remember setting any reference to null except for a few in my submission.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic