I'm using the LWJGL libraries and unfortunately I need to free up texture/vbo buffers myself whenever a node in my scene graph needs to die, I can't even use finalize() method to do this as I can't guarantee that it will be executed in the same thread the opengl libs expects it to.
So I'm using PhantomReferences. In my scene graph nodes I've put this in the constructor:
phantomReference = new ScenePhantomReference(this, Game.phantomReferenceQueue);
Game.phantomReferenceList.add(phantomReference);
As you can see in the second line I've added the phantomReference to a list in the main class. My logic is that when the node becomes dereferenced the phantomReference won't get garbage collected with it as there's still a reference in the main class.
Is adding it to a list like this needed? Or will it be spared from the GC (maybe the Game.phantomReferenceQueue keeps a reference to it?).
This one is a pain to test, I could just remove the list but the GC could just be processing the object that's being watched before the phantomReference and make it look like the list is redundant, when it really isn't. I'd be paranoid that any different VM implementation or version might decide to do it the other way round.
Disclaimer: I have not ever used PhantomReference.
However, I did read this article and this javadoc page, and so
Edit: Read the title of your post again. To answer the title question by itself: No, because a PhantomReference enters the ReferenceQueue after it has been gc'ed - so by definition it cannot be stopped from being gc'ed. To quote the first link: "The only use for such a reference [note: he means PhantomReference] is keeping track of when it gets enqueued into a ReferenceQueue, as at that point you know the object to which it pointed is dead"
Edit #2: I also think your code is wrong in that you should be initialising the phantom reference as follows (see a simple example here):
PhantomReference scenePhantomRef = new PhantomReference(scene, phantomQueue);
where scene
is the ScenePhantomReference
from your code (i.e. you should refactor your code so that ScenePhantomReference
is name e.g. Scene
, you instantiate it as scene
and then use the line above to get a handle on a PhantomReference
for that object).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With