Garbage Collection

Discussion in 'Java' started by Alex Bushell, May 27, 2004.

  1. Alex Bushell

    Alex Bushell Guest

    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( );
    }

    ....
    Alex Bushell, May 27, 2004
    #1
    1. Advertising

  2. Alex Bushell

    Tony Morris Guest

    "Alex Bushell" <> wrote in message
    news:...
    > 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)
    Tony Morris, May 27, 2004
    #2
    1. Advertising

  3. 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.
    Michael Borgwardt, May 27, 2004
    #3
  4. 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.
    Michael Borgwardt, May 27, 2004
    #4
  5. Alex Bushell

    Chris Smith Guest

    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
    Chris Smith, May 27, 2004
    #5
  6. Alex Bushell

    P.Hill Guest

    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
    P.Hill, May 27, 2004
    #6
  7. 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
    John C. Bollinger, May 27, 2004
    #7
  8. Alex Bushell

    Tony Morris Guest

    > 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
    Tony Morris, May 27, 2004
    #8
  9. Alex Bushell

    Roedy Green Guest

    On Thu, 27 May 2004 11:58:52 +0200, Michael Borgwardt
    <> 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.
    Roedy Green, May 28, 2004
    #9
  10. 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.
    Michael Borgwardt, May 28, 2004
    #10
  11. On Fri, 28 May 2004 10:10:48 +0200, Michael Borgwardt wrote:
    > 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.


    It seems you have to run it in that case. If you expect that you'll
    create threads and later change your mind, you could always set a
    field checked by the run method, then run the thread anyway:

    public void run() {
    if (!cancelled) {
    ...
    }
    }

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, May 28, 2004
    #11
  12. Alex Bushell

    xarax Guest

    "Roedy Green" <> wrote in message
    news:...
    > On Thu, 27 May 2004 11:58:52 +0200, Michael Borgwardt
    > <> 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


    There is nothing the JavaDoc for Thread or ThreadGroup
    that indicates that a JVM implementation is required
    to hold an inactive Thread reference in the ThreadGroup.

    When instantiating the Thread, there is nothing that
    indicates that the Thread instance is referenced in the
    ThreadGroup before the thread is started. The Thread
    instance may hold a reference to the ThreadGroup to
    which the thread will belong after the start() method
    is called. The start() method could add the Thread
    reference to the ThreadGroup at that time.

    Your glossary is quite likely describing a particular
    implementation detail that can change across different
    JVM releases and almost certainly across different JVM
    vendors.
    xarax, May 28, 2004
    #12
  13. Alex Bushell

    Neal Gafter Guest

    Roedy Green wrote:
    > 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.


    In fact, JDK 1.5.0 does not retain unstarted threads that otherwise become
    unreachable. See <http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4089701>.
    Neal Gafter, May 28, 2004
    #13
  14. Alex Bushell

    Roedy Green Guest

    On Fri, 28 May 2004 14:09:42 GMT, "xarax" <> wrote or
    quoted :

    >There is nothing the JavaDoc for Thread or ThreadGroup
    >that indicates that a JVM implementation is required
    >to hold an inactive Thread reference in the ThreadGroup.


    Correct. But it does.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, May 28, 2004
    #14
  15. Alex Bushell

    Chris Smith Guest

    Roedy Green wrote:
    > On Fri, 28 May 2004 14:09:42 GMT, "xarax" <> wrote or
    > quoted :
    >
    > >There is nothing the JavaDoc for Thread or ThreadGroup
    > >that indicates that a JVM implementation is required
    > >to hold an inactive Thread reference in the ThreadGroup.

    >
    > Correct. But it does.


    The conveniently missed point here is that this statement is meaningless
    unless you define "it". If "it" is a Sun release of the JVM prior to
    1.5, then you're right.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, May 28, 2004
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laser Lu

    Garbage Collection and Manage Code?

    Laser Lu, Jan 26, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    710
    Gaurav Khanna [C# MVP]
    Jan 27, 2004
  2. Cheung, Jeffrey Jing-Yen
    Replies:
    3
    Views:
    1,547
    Cheung, Jeffrey Jing-Yen
    Jul 10, 2003
  3. IDoNothing

    Garbage Collection in ASP.NET

    IDoNothing, Jan 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    8,362
    cjohn
    Aug 20, 2009
  4. Replies:
    1
    Views:
    437
    mrstephengross
    Jul 25, 2005
  5. Øyvind Isaksen
    Replies:
    1
    Views:
    960
    Øyvind Isaksen
    May 18, 2007
Loading...

Share This Page