jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes Why to call remove Listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why to call remove Listeners " Watch "Why to call remove Listeners " New topic
Author

Why to call remove Listeners

Bhanu
Ranch Hand

Joined: Dec 05, 2004
Posts: 48
I've this question lingering in mind since one of my Sr. team mate told me that. He asked me to remove the listeners before disposing/closing the frame. He says, if we don't remove then listeners will not be collected by GC at all. That didn't make any sense to me. GC will check the reachabilty of all objects and it would collect any unused objects when it finds them.
I would be happy to know If there is a valid reason behind my Sr's thumbrule.


Thanks,<br />Bhanu<br />SCEA Step-I<br />SCBCD for J2EE 1.3<br />SCJP1.4
Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
Bhanu,

I'm not sure the specifics of how the references are stored when you add a listener, but what could possibly be happening is known as the island of isolation. When you have two (or more objects) that are only referencing each other, such that A holds a reference to B and B holds a Reference to A, they will not be garbage collected. Make sense?

Jeff
Bhanu
Ranch Hand

Joined: Dec 05, 2004
Posts: 48
Thanks Jeff, But I don't agree with your assumption. An object is considered as unreachable by GC if it's not reachable by any strong references.

Let's take the same example picked by you. If A and B are referenced by each other and are not referenced by any Object that's is in use by a Thread then GC consider them a collectable.
Please check the following link.
http://java.sun.com/developer/technicalArticles/ALT/RefObj/
Jessica Dancel
Greenhorn

Joined: Dec 07, 2006
Posts: 10
Bhanu,
I was told the same thing by my manager. He said if you don't remove the listeners it can cause a memory leak. Must be some truth to it. Could be that the garbage collector takes a long time to find those references.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Hmmm... I think this is just a Java "Urban Legend" - as far as I can tell nothing of the sort is happening.

The JVM GC is built to correctly handle garbage collecting circular references - no "island of isolation" problem.

Strong references vs. weak references have nothing to do with the problem - all the references in this case (as far as I know) would be strong references. Weak references are for special cases when you want the GC to behave different than normal. Normal GC behavior works in this case.

There may be some "special case" that doesn't get GC'ed, or there may have been at some point in the past a bug that caused this, or there may be some bad code in the specific application you're working on to cause this - but in general - there is no reason to do this.

Now it's MythBusters time...

Program illustrating there is no difference in GC'ing listener references if you remove listeners before disposing of the frame, or leave them:



Output from running this:


(NEP - fixed output)

[ December 14, 2006: Message edited by: Nathan Pruett ]
[ December 14, 2006: Message edited by: Nathan Pruett ]

-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Bhanu
Ranch Hand

Joined: Dec 05, 2004
Posts: 48
Thanks Nathan!! I'm convinced. It was a Myth and your program busted it.
Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
Hi again. Sorry I've been slow to respond - been away for a bit. Anyway Nathan, I ran your example and I get different results. I only get the finalize method running when I run option 2, where the listeners are removed first. When I run option 1, the finalize methods never get called. Could this be a difference across JVMs? different optimizations? etc

Jeff
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Which JVM version did you run it under?
Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
Hi Nathan,

Sorry for the slow response again - it's that time of year. Anyway, I ran it under Java 5 (1.5.0_09-b01).

Jeff
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I originally ran it under 1.4.2, but I installed the 1.5.0_09 JRE to check - I got that the references were GC'ed the same way as they originally were...

I'm running on Win2k... what platform are you running on?

Are you using any command line options on the JRE? Especially ones that affect GC?

Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
I'm running on Windows XP, and no command line options at all.

Jeff
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I'll try this on XP later when I get home... can you provide a capture of the command-line output (like above) - just to make sure all steps are being run?
Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
Attached is the output



Jeff
[ January 15, 2007: Message edited by: Jeff Storey ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Note that calling System.gc() is just a *recommendation* to start a gc'ing cycle. There is no guarantee that there actually happens a gc cycle, or that it will be full one. You might want to try calling GC more than once in succession.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
Ilja,

I am aware that System.GC does not force a garbage collection. I was just illustrating that the GC behavior is not the same, which I think I have done.

Jeff
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Several years later, I came across this post I had forgotten about. I realized that I was in fact wrong on the behavior of the island of isolation. Just wanted to clarify here in case anyone else stumbles across this.


Jeff Storey
Software Developer
[url]http://jeffastorey.blogspot.com[/url]
 
wood burning stoves
 
subject: Why to call remove Listeners
 
Similar Threads
Explicit Garbage Collection invocation
Garbage Collection
Garbage collection
How many object is eligible for garbage collection?
GC question from Khalid