11

is there an elegant way in Java to set all existing object-pointers to null? I want an object to be deleted, but it is registered in several places an Event Listener.

Skip
  • 6,240
  • 11
  • 67
  • 117
  • 1
    why not just delete the Event Listener? – emory Aug 02 '11 at 19:36
  • yes, one possible solution would be to delete all references from all EventListeners, when an object was deleted, but using [WeakReferences](http://download.oracle.com/javase/1,5.0/docs/api/java/lang/ref/WeakReference.html), which will self destroy when the Object is set to null, is more elegant – Skip Aug 02 '11 at 19:42

5 Answers5

8

There is no elegant way for that. Don't try to do it or you will end up with difficult to maintain code.

Michał Šrajer
  • 30,364
  • 7
  • 62
  • 85
8

You can take a look at References. If you use a reference to refer to the class, it will not prevent an object from being garbage collected when all of the regular references are removed. The references held by the Reference class doesn't count when the collector determines which objects to collect. Of course, the listeners will have to correctly handle the reference suddenly disappearing. And you'll have to make sure something's keeping a regular reference for as long as you need to class to stick around.

Check out this thread for a discussion about the differences between the soft and weak references. What is the difference between a soft reference and a weak reference in Java?

Community
  • 1
  • 1
sblundy
  • 60,628
  • 22
  • 121
  • 123
5

Not explicitly, no. And there's a good reason for that - it's just not the way garbage collection works in Java!

If you really want to do this (though I'm saying this mostly for academic purposes, I'd strongly advise not doing it) the best approach I can think of is potentially wrapping up all references to the object in WeakReferences, and pass those around, just holding onto one strong reference to your object. If you set that strong reference to null then all the weak references to that object become eligible for garbage collection.

Of course, you need to be aware that as soon as the hard reference is set to null, the WeakReferences could disappear randomly at any time afterwards. So you need to make sure all your code using them is ready to handle that.

That said, that route is almost not the best to go down, and will make your code a lot harder to maintain! Ensuring that they're all released as they go out of scope nicely would be by far the better alternative, and shouldn't be hard if the rest of your code is neat enough.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
2

A few notes:

  1. You can not control object deletion in Java. This a job of garbage collector. You can instruct GC to do garbage collect, but this does not guarante object will be in fact deleted.

  2. In practical terms object is deleted when it can not be reached anymore. If you delete all references to it then you can not reach it anymore.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • Thats why I asked for the way to delete all object references – Skip Aug 02 '11 at 19:37
  • Well you can't - that's basically my answer. You could create your wrapper class that retrieves reference of object when needed, but you would have to make sure that code using this wrapper does not store the reference internally. – Peter Knego Aug 02 '11 at 19:41
1

With Java, you can't do that with references. You could create a deterministic Dispose method that empties the values in a reference like .NET's IDisposable.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445