aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Listeners and GC Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Listeners and GC" Watch "Listeners and GC" New topic
Author

Listeners and GC

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14669
    
  11

When adding listeners to components (e.g. addActionListener), do we need to remove them explicitly afterwards, or will the garbage collector clear them properly when a window is closed ?

I'm using a profiler and it seems that the listener stays around unless I explicitly remove it from its source. If anybody could share some light on this.
[ March 29, 2007: Message edited by: Satou kurinosuke ]

[My Blog]
All roads lead to JavaRanch
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You shouldn't have to remove them explicitly - they should be GCed when the component(s) they are connected to are GCed.

Of course, a window "closing" may not GC any components, depending on what kind of "close" it is. Just calling setVisible(false) or dispose() doesn't GC a frame - just like any other object reference, the frame reference must be nulled, go out scope, etc. to be GCed.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14669
    
  11

Thank you.
But looking at the heap, listeners really seem to be floating around if I don't remove them. I manage opened frames in a collection. I'll check that references are nulled out properly on closing.
 
I agree. Here's the link: http://jrebel.com/download
 
subject: Listeners and GC
 
Similar Threads
container Repaint problem
K&B Chapter 1 Question 5
Clarification
How do I get this listener to wait for this other listener?
Why to call remove Listeners