check if referenced elswhere/enforced garbagecollection
I have a LinkedList that contains some instances of some classes.
At some point i want to go through all of them and invoke a method they all have.
Now it might also be that some of them are no longer necessary at a given point of time. That would be when there is no other reference to that instance then from this List. In that case i would like to throw them out of the list so they can be garbagecollected.
Is there a way to create a reference that goes to null if there are no longer any further references to that object?
Is there a method to check if that object is also referenced elswhere in the programm?
[652 byte] By [
arcosh] at [2007-9-26 2:37:37]

You can use java.lang.ref.WeakReference to get exactly this behavior. When the garbage collector determines that an object is no longer referenced by normal references, it will set all WeakReferences to that object to null.
Keep in mind, however, that you cannot know exactly when the garbage collector will run, and therefore the WeakReference may have a non-null value even though the object is no longer referenced. This is because it's only when the garbage collector runs that the determination is made whether an object is reachable or not.
If you tried to determine at a specific time whether an object was reachable, the JVM would have to stop all threads and examine all references to see if they point to the object in question. This would be such a time-consuming operation that it is not provided.