This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "GC doubt" Watch "GC doubt" New topic
Author

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
Rancher

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

Joined: Apr 06, 2010
Posts: 4341
    
    7

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: http://aspose.com/file-tools
 
subject: GC doubt
 
Similar Threads
Compiling errata for K&B, SCJP 6
Quick Q on String and GC
Garbage Collection
Garbage Collection (URGENT)!