Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

creating a new instance

Thread view: 
Tony Johansson - 24 May 2005 14:03 GMT
Hello!!

Here I create a new instance t of class Test and pass the referenc to method
goo.
When this method foo eventually finish will this instace t go out of scope
and be garbage collected.?
If not exactly what must occur so that the instance t be garbage collected.

void foo()
{
   Test t = new Test;
   goo(t);
}

//Tony
Claus Petersen - 24 May 2005 14:14 GMT
> Hello!!
>
[quoted text clipped - 12 lines]
>
> //Tony

Short anwer: YES.......

But if you want to be sure, then set t = null; after the call to goo

Claus
Casper B - 24 May 2005 15:40 GMT
> But if you want to be sure, then set t = null; after the call to goo

And (assuming everything is syncronous) if you REALLY want to be in
control, call the garbage collector explicitly:

System.gc();

/Casper
Alex Molochnikov - 25 May 2005 16:53 GMT
> > But if you want to be sure, then set t = null; after the call to goo
>
[quoted text clipped - 4 lines]
>
> /Casper

... Except that the GC will never let you be in control. The call to
System.gc() is only a _suggestion_ to JVM to run the garbage collector. The
JVM may or may not do it on your request. Also, setting t to null is
superfluous. Once t goes out of scope in the given example, all references
to it are gone, and it becomes eligible for the collection - whether it was
nulled out or not.
Raymond DeCampo - 24 May 2005 17:58 GMT
>>Hello!!
>>
[quoted text clipped - 18 lines]
>
> Claus

It depends also on what goo() does.  If goo() stores a reference
somewhere t may not be eligible for GC.

Ray

Signature

XML is the programmer's duct tape.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.