Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

destroy object

Status
Not open for further replies.

almoes

Programmer
Jan 8, 2003
291
0
0
US
Hi,

Is there a way of totally destroying an object? I mean, if you remove the instance it doesn't really kill it and in my case it's a listener of events so it still reacts upon them. Any ideas? Garbage collector is not fast enough :-(

cheers,
alej
 
In the future, please post non-J2EE questions in forum269

In short ... no there isn't - Java does not support destructors or finalizers.

If you have a listener that needs to be shut down, you'll have to code it so that you can either tell the listener to shut down, or for your code that is notified by that listener, to have the ability to remove the listener from their scope.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
That's because you just destroy one reference, but the object is still referenced by the event dispatcher.

You should remove the listener from the dispatcher and the events won't be notified anymore.

Cheers,
Dian
 
If your listener still receives events because it's still registered with a 'dispatcher' object, it will never be garbage collected anyway. The dispatcher object clearly still has a reference to it.

As sedj and Dian have said, the correct way to write Java code like this is to manage your objects properly, not worry about how to force the GC to remove objects for you.

If a listener is no longer needed, it should be removed from all the objects it was registered with. If a class has an 'addListener' method, it will usually have a 'removeListener' method as well for just this purpose. If you've written the dispatcher class and it doesn't have such a method then you should add one.

If your listener object is then truly unneeded (you've removed it from all referencing objects - dispatchers and containers etc), then the CG will attend to it in due course as an unreferenced object.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top