• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Component not being garbage collected

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am working with an existing custom component that updates itself periodically. The component resides in its own JFrame which can be launched from my main application. The problem I'm having is that the component is not being garbage collected after its window has been closed. I've posted some simple sample code below. This is not the actual code, but it illustrates the issue.

The main class...


The custom component...


In the sample code, a Swing Timer is used to periodically update the label text. I also print out the value of count. But when the secondary frame is closed, the custom component continues to print out the value.

What am I doing wrong? How can I get the component to go out of scope and be garbage collected. What is the best practice for doing this type of thing? I would appreciate any insight. Thanks.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just ran some tests and saw that a running Timer continues to run even when no longer in scope. Didn't go too deep in the sources, but it's probably still being referenced by the TimerQueue.

I suggest you add a ComponentListener to your custom component and stop() the Timer on componentHidden(...). OF course, after first makng the Timer an instance field instead of local to the constructor.Another approach could be to test the component's isDisplayable() in the Timer's actionPerformed.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After some more thinking about this, there's yet another approach that gives you finer control over when the Timer is stop()ped. That is to add a public void dispose() method to your CustomLabel that calls timer.stop() and invoke that method from your own code before the reference to the CustomLabel goes out of scope.

EDIT
Also with reference to your subject line, a Timer wouldn't stop when your references to it go out of scope. Think about it. When a Timer is created with a local variable referencing it, would you expect that Timer to stop firing events when the method in which it was declared returns? Of course not.

Timer#start() adds the timer to the TimerQueue and stop() removes it. So, as long as the timer is running the TimerQueue retains a reference to it and it can be GCed only after being stopped. Read the sources if you're interested in seeing where that's done.

Hope that makes things a little clearer.
 
J. Ryan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Darryl.

After looking at the source for javax.swing.Timer, it makes sense to me now. I like the approach of adding a dispose() method. I can easily call it when the window is closed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic