• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to destroy Singleton Object ?

 
Ashwin Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody tell me how to destroy Singleton Object with out closing the application ??
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a clear() method...

 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Dave - sounds like you probably shouldn't use the Singleton pattern.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ryan McGuire
Ranch Hand
Posts: 1081
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic