Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Garbage Collection

Reply
Thread Tools

Garbage Collection

 
 
Alex Bushell
Guest
Posts: n/a
 
      05-27-2004
Is this legal Java, and when is the Thread instance collected...

....

private SomeObjectThatImplementsRunnable _an_object = new
SomeObjectThatImplementsRunnable;

....

public void someMethod( )
{
new Thread( _an_object ).start( );
}

....
 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      05-27-2004

"Alex Bushell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Is this legal Java, and when is the Thread instance collected...
>
> ...
>
> private SomeObjectThatImplementsRunnable _an_object = new
> SomeObjectThatImplementsRunnable;
>
> ...
>
> public void someMethod( )
> {
> new Thread( _an_object ).start( );
> }
>
> ...


Yes - it is legal (Assuming the missing body, there is no compile-time
error).
The Thread instance will be garbage collected when the object becomes
eligible for garbage collection and whenever the GC decides it is time.
This can be said about any object instance.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
(2003 VTR1000F)


 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      05-27-2004
Alex Bushell wrote:

> Is this legal Java,


Except for the syntax errors, yes.

> and when is the Thread instance collected...


It becomes *egligible* for garbage collection when its run() method
has finished executing. When it will actually *be* garbage collected
is up to the JVM to decide.
 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      05-27-2004
Tony Morris wrote:

>>private SomeObjectThatImplementsRunnable _an_object = new
>>SomeObjectThatImplementsRunnable;
>>
>>...
>>
>>public void someMethod( )
>>{
>> new Thread( _an_object ).start( );
>>}
>>
>>...

>
>
> Yes - it is legal (Assuming the missing body, there is no compile-time
> error).


No. the first line is not legal syntax (missing parentheses for
constructor arguments).


> The Thread instance will be garbage collected when the object becomes
> eligible for garbage collection and whenever the GC decides it is time.
> This can be said about any object instance.


i.e. it's a truism that doesn't answer the question.

Running Thread objects are among the *roots* of the reachability trees used in
today's garbage collectors.
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-27-2004
Michael Borgwardt wrote:
> > and when is the Thread instance collected...

>
> It becomes *egligible* for garbage collection when its run() method
> has finished executing. When it will actually *be* garbage collected
> is up to the JVM to decide.


It would be more accurate to say that it will not be eligible for
garbage collection until after its run() method is completed. Thread
instances are not immune to general considerations of reachability, so
the thread is free to store off references to its Thread instance, which
could be obtained from Thread.currentThread(), where ever it likes,
which could cause that Thread instance to greatly outlive the thread
itself.

In any case, it's rarely of use to enumerate all the conditions for a
specific object to become eligible for garbage collection. You're
better off going by the geberal rule that an object is always eligible
for garbage collection the instant that it becomes inaccessible. Since
the current Thread instance is accessible for a running thread (via
Thread.currentThread()), everything said so far falls within that
statement... but it's not nearly so complex as all that.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      05-27-2004
Chris Smith wrote:
> but it's not nearly so complex as all that.


I have to agree with Chris, it certainly isn't so complex.

My hunch is that the OP was wondering that since HE didn't hold
onto a reference to it, he was expecting something else surely
had reference to it and was hoping for a reasonable programmers
model which he could think about to understand when the thread
really does become eligible. I assume the various others in this
thread have provided enough information.

-Paul

 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      05-27-2004
P.Hill wrote:

> My hunch is that the OP was wondering that since HE didn't hold
> onto a reference to it, he was expecting something else surely
> had reference to it and was hoping for a reasonable programmers
> model which he could think about to understand when the thread
> really does become eligible. I assume the various others in this
> thread have provided enough information.


And I suspect that the particular thing he worried about is that the
Thread, its Runnable, or some of the other Objects it works on might be
GC'd before the Thread was done, thus leading to corrupt data, execution
of random instructions, or other badness. That cannot ever happen in
Java. (Which is to say that it would represent a JVM bug if it ever did
happen.)

--
John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      05-27-2004
> No. the first line is not legal syntax (missing parentheses for
> constructor arguments).


An assumed oversight by the OP - apologies for not stating so.

> i.e. it's a truism that doesn't answer the question.


It answers the question in as much as there was never any intent shown for a
more detailed answer.

> Running Thread objects are among the *roots* of the reachability trees

used in
> today's garbage collectors.



--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-28-2004
On Thu, 27 May 2004 11:58:52 +0200, Michael Borgwardt
<(E-Mail Removed)> wrote or quoted :

>It becomes *egligible* for garbage collection when its run() method
>has finished executing. When it will actually *be* garbage collected
>is up to the JVM to decide.


Be careful. If you instantiate a Thread but never get around to
calling the start method, it will hang around forever,
even if you drop all references to it.

see http://mindprod.com/jgloss/packratting.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      05-28-2004
Roedy Green wrote:
>>It becomes *egligible* for garbage collection when its run() method
>>has finished executing. When it will actually *be* garbage collected
>>is up to the JVM to decide.

>
>
> Be careful. If you instantiate a Thread but never get around to
> calling the start method, it will hang around forever,
> even if you drop all references to it.


Very interesting detail. Then what can you do to reclaim the memory if you
come to a point where you have instantiated a Thread but change your mind
about running it? I can't seem to find anything in the Thread class that
could be used to handle this case.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Templates - Garbage In Garbage Not Out ramiro_b@yahoo.com C++ 1 07-25-2005 04:48 PM
Garbage Collection kamran MCSD 1 04-04-2005 10:04 PM
Garbage Collection and Manage Code? Laser Lu ASP .Net 5 01-27-2004 03:48 AM
Debbugging help! (.NET 1.1 Framework Garbage Collection Problems) Cheung, Jeffrey Jing-Yen ASP .Net 3 07-10-2003 07:29 PM



Advertisments