aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Component not being garbage collected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Component not being garbage collected" Watch "Component not being garbage collected" New topic
Author

Component not being garbage collected

J. Ryan
Greenhorn

Joined: Jul 24, 2006
Posts: 21
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.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

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

Joined: Jul 24, 2006
Posts: 21
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Component not being garbage collected
 
Similar Threads
What is the right way to flash a lable?
input errors in JDialogs
Redirecting of one frame to another frame
Am i making use of classes in the right way?
Problem with custom painting and multiple JPanel