Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why to call remove Listeners

 
Bhanu
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bhanu
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nathan!! I'm convinced. It was a Myth and your program busted it.
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which JVM version did you run it under?
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm running on Windows XP, and no command line options at all.

Jeff
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Attached is the output



Jeff
[ January 15, 2007: Message edited by: Jeff Storey ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic