Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Swing / AWT / SWT and the fly likes Memory Leaks and Removing ActionListeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Memory Leaks and Removing ActionListeners" Watch "Memory Leaks and Removing ActionListeners" New topic

Memory Leaks and Removing ActionListeners

Hollowman Kumar

Joined: May 14, 2003
Posts: 1
I have a button declared at the class level
i instantiate the button in the constructor and add an actionListener object to the same. while moving to the next screen if i set the button to null would the button be eligible for garbage collection or do i explicitly need to remove the actionListener object as well?
Thank You,
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

You don't have to remove the actionlistener, for two reasons...
  • ActionListeners don't (normally) have a reference to the components they are attached to
  • Even if the ActionListener did have a reference to the button, as long as there wasn't another reference to the ActionListener, they would still be garbage collected. The GC is able to figure out circular dependencies.

  • -Nate
    Write once, run anywhere, because there's nowhere to hide! - /. A.C.
    Have you tried LearnNowOnline?
    subject: Memory Leaks and Removing ActionListeners