TimerTask destruction

Discussion in 'Java' started by Jeff, Aug 19, 2004.

  1. Jeff

    Jeff Guest

    For a one time Timer, after the TimerTask's run method is executed, is the
    TimerTask available to be garbage collected? I'm having a hard time proving
    it, but I think the TimerTask persists after run() is executed. I could fix
    it by explicitly calling .cancel() from the TimerTask, but I suspect the
    language designers had a better solution in mind.

    In this example, is FireJeffAlarm eligable for garbage collection after it's
    run() is done?

    Thanks

    class JeffTimer extends java.util.Timer {

    JeffTimer() {
    schedule( new FireJeffAlarm() , 2000 );
    System.out.println( " scheduled alarm" );
    }

    class FireJeffAlarm extends TimerTask {
    public void run() {
    System.out.println( "firing Jeff Alarm " );
    } // run

    } // FireJeffAlarm

    public static void main( String args[] ) {
    JeffTimer timer = new JeffTimer();
    }

    }



    --
    Jeffrey Drew
    President and Founder
    Trading Metrics, Inc.
    917-453-0302
     
    Jeff, Aug 19, 2004
    #1
    1. Advertising

  2. Jeff

    Paul Lutus Guest

    Jeff wrote:

    > For a one time Timer, after the TimerTask's run method is executed, is the
    > TimerTask available to be garbage collected? I'm having a hard time
    > proving
    > it, but I think the TimerTask persists after run() is executed. I could
    > fix it by explicitly calling .cancel() from the TimerTask, but I suspect
    > the language designers had a better solution in mind.
    >
    > In this example, is FireJeffAlarm eligable for garbage collection after
    > it's run() is done?


    "eligable" -> eligible

    There's a way to find out:

    1. Create and run the timer. Let it run to completion, however you define
    that.

    2. In a separate repeating Timer thread (or a GUI with comand buttons),
    monitor the status of the object (you will have to assign the TimerTask
    object in a way that you are not doing now).

    3. Call System.gc(), then, later, test for the presence or absence of a
    valid TimerTask object.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Aug 19, 2004
    #2
    1. Advertising

  3. On Thu, 19 Aug 2004 13:53:17 GMT, Jeff wrote:
    > For a one time Timer, after the TimerTask's run method is executed,
    > is the TimerTask available to be garbage collected? I'm having a
    > hard time proving it, but I think the TimerTask persists after run()
    > is executed. I could fix it by explicitly calling .cancel() from the
    > TimerTask, but I suspect the language designers had a better
    > solution in mind. In this example, is FireJeffAlarm eligable for
    > garbage collection after it's run() is done?


    Have a look at the implementation of Timer, or more speicfically, the
    implementation of the TaskQueue inner class. The source comes with the
    JDK.

    The TimerTask is removed from the queue when it fires, and the queue
    position it occupied is set to null.

    You can't predict when gc will occur, so don't expect the object to be
    collected immediately after it runs (or even soon after if there is
    lots of memory available).

    /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, Aug 19, 2004
    #3
  4. Jeff

    Jeff Guest

    Thanks for the pointer to TaskQueue. That's very helpful. I'll look at
    that code.

    "Gordon Beaton" <> wrote in message
    news:4124b9cc$...
    > On Thu, 19 Aug 2004 13:53:17 GMT, Jeff wrote:
    > > For a one time Timer, after the TimerTask's run method is executed,
    > > is the TimerTask available to be garbage collected? I'm having a
    > > hard time proving it, but I think the TimerTask persists after run()
    > > is executed. I could fix it by explicitly calling .cancel() from the
    > > TimerTask, but I suspect the language designers had a better
    > > solution in mind. In this example, is FireJeffAlarm eligable for
    > > garbage collection after it's run() is done?

    >
    > Have a look at the implementation of Timer, or more speicfically, the
    > implementation of the TaskQueue inner class. The source comes with the
    > JDK.
    >
    > The TimerTask is removed from the queue when it fires, and the queue
    > position it occupied is set to null.
    >
    > You can't predict when gc will occur, so don't expect the object to be
    > collected immediately after it runs (or even soon after if there is
    > lots of memory available).
    >
    > /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
     
    Jeff, Aug 19, 2004
    #4
  5. Jeff

    Liz Guest

    "Jeff" <> wrote in message
    news:hh2Vc.26373$iE3.21621@trndny09...
    > For a one time Timer, after the TimerTask's run method is executed, is the
    > TimerTask available to be garbage collected? I'm having a hard time

    proving
    > it, but I think the TimerTask persists after run() is executed. I could

    fix
    > it by explicitly calling .cancel() from the TimerTask, but I suspect the
    > language designers had a better solution in mind.
    >
    > In this example, is FireJeffAlarm eligable for garbage collection after

    it's
    > run() is done?


    After it is done you can get the list of threads and see if it is still
    there.

    >
    > Thanks
    >
    > class JeffTimer extends java.util.Timer {
    >
    > JeffTimer() {
    > schedule( new FireJeffAlarm() , 2000 );
    > System.out.println( " scheduled alarm" );
    > }
    >
    > class FireJeffAlarm extends TimerTask {
    > public void run() {
    > System.out.println( "firing Jeff Alarm " );
    > } // run
    >
    > } // FireJeffAlarm
    >
    > public static void main( String args[] ) {
    > JeffTimer timer = new JeffTimer();
    > }
    >
    > }
    >
    >
    >
    > --
    > Jeffrey Drew
    > President and Founder
    > Trading Metrics, Inc.
    > 917-453-0302
    >
    >
     
    Liz, Aug 20, 2004
    #5
    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. John C. Bollinger
    Replies:
    0
    Views:
    471
    John C. Bollinger
    Aug 18, 2003
  2. Liz

    TimerTask priority

    Liz, Apr 17, 2004, in forum: Java
    Replies:
    3
    Views:
    3,060
    Roedy Green
    Apr 18, 2004
  3. iksrazal

    TimerTask inside Runnable?

    iksrazal, Jun 29, 2004, in forum: Java
    Replies:
    5
    Views:
    5,245
    iksrazal
    Jul 1, 2004
  4. tkonrath
    Replies:
    4
    Views:
    6,246
    Roedy Green
    Oct 17, 2005
  5. Replies:
    0
    Views:
    335
Loading...

Share This Page