在ReferenceQueue中的PhantomReference是否会阻止PhantomReference进行GC? [英] Does a PhantomReference being in a ReferenceQueue stop the PhantomReference from being GC'd?

查看:147
本文介绍了在ReferenceQueue中的PhantomReference是否会阻止PhantomReference进行GC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LWJGL库,不幸的是,每当我的场景图中的节点需要死亡时,我都需要释放纹理/ vbo缓冲区,我甚至无法使用finalize()方法来执行此操作。 t保证它将在opengl lib期望的同一个线程中执行。

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.

所以我正在使用PhantomReferences。在我的场景图节点中,我把它放在构造函数中:

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);

正如您在第二行中看到的,我已将phantomReference添加到主类中的列表中。我的逻辑是,当节点被解除引用时,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.

将它添加到列表中这需要吗?或者它会从GC中幸免(可能Game.phantomReferenceQueue会保留对它的引用吗?)。

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?).

这个很难测试,我可以删除列表,但GC可能只是处理phantomReference之前正在观看的对象,并使它看起来像列表是多余的,当它真的不是。我很偏执,任何不同的VM实现或版本都可能决定反过来。

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.

推荐答案

免责声明:我还没有用过PhantomReference。

Disclaimer: I have not ever used PhantomReference.

但是,我确实读过这篇文章这个javadoc页面,所以

However, I did read this article and this javadoc page, and so


  1. 是否将它添加到需要的列表中? [...](也许Game.phantomReferenceQueue保留对它的引用?):根据javadoc页面:引用队列,在检测到适当的可达性更改后,垃圾收集器将附加的注册引用对象附加到该引用队列。 。所以我相信不,你不应该把它添加到List中,因为Queue会自动将它添加到垃圾收集器中。

  2. 我认为你必须重新解释一下你的问题 - 我不明白列表是什么,即我不理解最后一段。但是如果你正在使用列表来知道phantomReference是否是gc'ed然后没有,你根本就不应该使用列表,这就是ReferenceQueue的用途。此外,列表将阻止幻像参考被gc'ed!

编辑:再次阅读你的帖子的标题。单独回答标题问题:不,因为PhantomReference在gc'ed后进入ReferenceQueue - 因此根据定义它不能被停止为gc'ed。引用第一个链接:这个引用的唯一用途[注意:他的意思是PhantomReference]跟踪它何时被排入ReferenceQueue,因为那时你知道它指向的对象已经死了

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"

编辑#2:我还认为你的代码是错误的,你应该按如下方式初始化幻像参考(参见一个简单的例子这里):

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);

其中场景您的代码中的ScenePhantomReference (即您应该重构您的代码,以便 ScenePhantomReference 是名称,例如 Scene ,你将它实例化为 scene ,然后使用上面的行来获取 PhantomReference 的句柄对象)。

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).

这篇关于在ReferenceQueue中的PhantomReference是否会阻止PhantomReference进行GC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆