File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes GC doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "GC doubt" Watch "GC doubt" New topic

GC doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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 ?

Tell the difficulties that i am difficult.
E Armitage

Joined: Mar 17, 2012
Posts: 989
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

Joined: Apr 06, 2010
Posts: 4543

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

Joined: Feb 09, 2012
Posts: 583

thanks Matthew
I agree. Here's the link:
subject: GC doubt
It's not a secret anymore!