File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to destroy Singleton Object ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to destroy Singleton Object ?" Watch "How to destroy Singleton Object ?" New topic
Author

How to destroy Singleton Object ?

Ashwin Kumar
Ranch Hand

Joined: Apr 07, 2005
Posts: 78
Can anybody tell me how to destroy Singleton Object with out closing the application ??


Preparing SCEA..<br />SCBCD 5.0<br />SCWCD 1.4<br />SCJP 1.4
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
You could use a clear() method...



James Carman, President<br />Carman Consulting, Inc.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
You cannot destroy an object, only the JVM can do that.

Once an object is inaccessible by any live threads, it then becomes eligible for garbage collection.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Sounds like something which possibly shouldn't be a Singleton.

You can unload a Class by removing any references to its ClassLoader and allowing both to be garbage collected. It's easier to redesign the Class so this isn't required though.

Dave
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I agree with Dave - sounds like you probably shouldn't use the Singleton pattern.


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
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Although James' solution works. If that's what you need you'd probably better with a factory to hand out the various instances - ie not a Singleton.
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
The clear() idea will not work, because some other class may have already obtained a reference. Then if it is cleared, the next caller will get a different instance and you will have two instances of your "Singleton".

There is no way to destroy a Singleton without breaking the Singleton property.

Geoffrey


Sun Certified Programmer for the Java 2 Platform
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1007
    
    3
As others have said, maybe a Singleton isn't appropriate here. If the number of instances of the class can go down to zero, then it's not a Singleton.

Instead of destroying the existing object and instantiating a new one, could you just change the internal state of the one that already exists? Or would that mess up any client objects that already have a reference to it?

What is this class/object used for?

Ryan
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
What behavior do you expect? How would you know that it's been destroyed? What would happen?

I kinda liked James Carman's first response that when you ask for the instance you get nothing. That's a sign that it's no longer there even though it may be in memory a while before GC. If you had to load it from the class file again for some reason you could just use a new classloader (maybe a custom one) and populate your "theOnlyInstance" variable again.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

Here's an interesting option... For the clear() method, move the instance to a weak reference. When the factory is called, it can try to get the reference back first. If it can't, then it can create a new one.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to destroy Singleton Object ?