Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC doubt

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K&B 6
Since the method returns the Date object, it will not be eligible for collection even after the method has completed

Then when d2 will be eligible ?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When there are no more live references to it.
The reference d in your main method is pointing to d2 after the method call so d2 will only be eligible after the main method has exited or removed all references to it (e.g if the main method sets d to null)
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that variables are not garbage collected. Objects are. So it doesn't make sense to talk about whether d2 is eligible or not. The question is whether the object that d2 referenced is eligible. And that will be, as E Armitage says, when there are no other live references. When getDate() exits you're assigning the return value (a reference to that same object) to the d variable, so it's still live until the program ends.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Matthew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic