简体   繁体   English

监视Java对象销毁

[英]Monitor Java object destruction

I have some objects that are created/destroyed very often and that can exist in many lists at the same time. 我有一些经常被创建/销毁的对象,它们可以同时存在于许多列表中。 To ensure I have no references left to them the objects have a flag isDestroyed, and if this is set, each list is responsible for removing the object from the list. 为了确保我没有任何引用,对象有一个标志isDestroyed,如果设置了这个,每个列表负责从列表中删除对象。

However this is ofcourse a growing ground for memory leaks. 然而,这是内存泄漏的一个增长点。 What if I forget to remove objects from one of the lists? 如果我忘记从其中一个列表中删除对象怎么办? To visually monitor that the program behaves correctly, I override finalize and increase a global variable to track destructions (not a formal test, only to get an idea). 为了直观地监视程序的行为是否正确,我重写finalize并增加一个全局变量来跟踪析构(不是正式的测试,只是为了得到一个想法)。 However as I have no control over the GC, I could in theory wait forever until something is destroyed. 然而,由于我无法控制GC,我理论上可以永远等待,直到某些东西被破坏。

So the question is two-fold: When having objects that are in multiple lists, is a "isDestroyed" considered a good way to control the object lifetime? 所以问题是双重的:当拥有多个列表中的对象时,“isDestroyed”被认为是控制对象生命周期的好方法吗? It forces everyone who uses the object to take care to remove it from their lists, which seems bad. 它强制每个使用该对象的人都要小心将其从列表中删除,这看起来很糟糕。

And, is there any good way to see when the reference count reaches zero on an object, ie when its scheduled for destruction? 并且,有没有什么好方法可以查看对象上的引用计数何时达到零,即当它被计划销毁时?

EDIT: To be more specific, in my case I my objects represent physical entities in a room. 编辑:更具体地说,在我的情况下,我的对象代表房间中的物理实体。 And I have one manager class that draws each object, therefore it is in one list. 我有一个绘制每个对象的管理器类,因此它在一个列表中。 Another list contains all the objects that are clickable, so there I have another list. 另一个列表包含可点击的所有对象,因此我有另一个列表。 Having all objects in one list and using polymorphism or instance of is not an option in this case. 在这种情况下,将所有对象放在一个列表中并使用多态或实例不是一个选项。 When a object is "destroyed", it should neither be shown or clickable in any way, therefore I want to remove it from both lists. 当一个对象被“销毁”时,它既不应该以任何方式显示或点击,因此我想从两个列表中删除它。

You should have a look at the java.lang.ref Package. 你应该看一下java.lang.ref包。

And, is there any good way to see when the reference count reaches zero on an object, ie when its scheduled for destruction? 并且,有没有什么好方法可以查看对象上的引用计数何时达到零,即当它被计划销毁时?

You can use the ReferenceQueue Object 您可以使用ReferenceQueue对象

From JavaDoc of java.lang.ref.ReferenceQueue 来自java.lang.ref.ReferenceQueue JavaDoc

Reference queues, to which registered reference objects are appended by the garbage collector after the appropriate reachability changes are detected. 参考队列,在检测到适当的可达性更改后,垃圾收集器将附加的注册引用对象附加到该引用队列。

I think this what WeakReference and ReferenceQueue is for - you create a WeakReference for the object you are tracking and associate it with a ReferenceQueue. 我认为这是WeakReference和ReferenceQueue的用途 - 您为要跟踪的对象创建WeakReference并将其与ReferenceQueue相关联。 Then you have another thread that processes WeakReference(s) as it is returned from ReferenceQueue.remove(). 然后你有另一个处理WeakReference的线程,因为它是从ReferenceQueue.remove()返回的。 WeakReference's are added to ReferenceQueue when the referenced objects is GC'd. 当引用的对象是GC时,WeakReference被添加到ReferenceQueue。 But can you give an example on what these lists you are trying to clean up when the referenced objects are dead? 但是,您可以举例说明在引用的对象死亡时您要清理这些列表的内容吗?

The way this is usually handled is through the Observer pattern. 通常通过Observer模式处理这种方式。 Each list attaches a destroy-listener that gets notified upon destruction. 每个列表附加一个destroy-listener,在被破坏时得到通知。 How this meshes with you architecture, I have no details to judge from. 这与你的架构有什么关系,我没有细节可以判断。

If you want to be notified I'm almost sure you need PhantomReference, read here: 如果您想收到通知,我几乎可以肯定您需要PhantomReference,请在此处阅读:

http://weblogs.java.net/blog/2006/05/04/understanding-weak-references http://weblogs.java.net/blog/2006/05/04/understanding-weak-references

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM