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
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?
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.
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 ]
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
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
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
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.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop