java 是否有可能在执行时看到对一个对象的所有引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6155464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:35:26  来源:igfitidea点击:

Is it possible see all the references to an object in execution time?

javaobjectnetbeansreference

提问by Renato Dinhani

Is there a way to see all the references to an object in execution time?

有没有办法在执行时查看对象的所有引用?

I'm using Netbeans, does this feature exist in it?

我正在使用 Netbeans,其中是否存在此功能?

EDIT: No problem in using profilers to do this, I only need know the references, no matters how.

编辑:使用分析器来做到这一点没问题,我只需要知道参考资料,无论如何。

采纳答案by Renato Dinhani

Ok, Netbeans show all the references to an object.

好的,Netbeans 显示了对一个对象的所有引用。

First, run the project in debug mode CTRL + F5, after, show the Loaded Classes Alt + Shift + 4or Window->Debug->Loaded Classes.

首先,在调试模式下运行项目CTRL + F5,之后,显示加载的类Alt + Shift + 4Window->Debug->Loaded Classes.

Choose the class will want to see the references and double click on it.

选择要查看参考文献的班级并双击它。

Pause the execution and there is.

暂停执行就可以了。

In the top is the attributes of the object, and in the bottom, all references to it.

顶部是对象的属性,底部是对它的所有引用。

In the bottom area where is "Referências" shows the references of the selected object

在“Referências”的底部区域显示所选对象的引用

回答by Peter Lawrey

If you dump the heap and analyse it you get find all the references. Profilers like VisualVM and YourKit can do this for you.

如果您转储堆并对其进行分析,您将找到所有引用。像 VisualVM 和 YourKit 这样的分析器可以为你做这件事。

However its not possible to determine this dynamically. If you want to know all the things which refer to an object you must maintain a collection these yourself.

然而,它不可能动态地确定这一点。如果你想知道所有引用一个对象的东西,你必须自己维护一个集合。

回答by Vincent Ramdhanie

In Netbeans you can use the Find Usagesfeature to see where a particular class may have been referenced inside of a particular project.

在 Netbeans 中,您可以使用Find Usages功能来查看特定项目内可能引用了特定类的位置。

From the Project Explorer, select the class and right click > Find Usages.

Project Explorer 中,选择类并右键单击 > Find Usages

The results looks a bit like the following image:

结果看起来有点像下图:

Find Usage Results

查找使用结果

回答by Jér?me Verstrynge

Sorry, was not clear. I want the references in the execution time. All the refereces of a created object

抱歉,没说清楚。我想要执行时的引用。创建对象的所有引用

Unfortunately, there is no such feature available in Java. But, there is a way of being notified that there is no more reference to an object at runtime.

不幸的是,Java 中没有这样的特性。但是,有一种方法可以通知在运行时不再引用对象。

The solution is to create a weak reference to the monitored object and associate it with a reference queue. When there will be no more hard reference to that object, the GC will sooner or later recollect it and enqueue the weak reference. You can check this with isEnqueued().

解决方案是创建一个对被监控对象的弱引用,并将其与一个引用队列相关联。当不再有对该对象的硬引用时,GC 迟早会重新收集它并将弱引用加入队列。您可以使用isEnqueued()进行检查。

If you provide more information about your issue, may be we can give more tips & tricks.

如果您提供有关您的问题的更多信息,我们可能会提供更多提示和技巧。

EDIT

编辑

To control all references to an oject, you may use the Proxy Pattern. Instead of setting references to your connection object, you create a proxy object containing a private instance of the connection object. Then, have your code call the proxy who will call the connection object itself, instead of having direct references to the connection.

要控制对一个对象的所有引用,您可以使用代理模式。不是设置对连接对象的引用,而是创建一个包含连接对象私有实例的代理对象。然后,让您的代码调用将调用连接对象本身的代理,而不是直接引用连接。

When you are done with the connection object, close it inside the proxy object. If other parts of the code still try to access that connection objects via the proxy, you will be able to detect it in the proxy when it is called.

完成连接对象后,将其关闭在代理对象内。如果代码的其他部分仍然尝试通过代理访问该连接对象,您将能够在调用它时在代理中检测到它。

It is a trick you can use to find which part of the code is still 'referencing' your object, so to speak.

这是一个技巧,您可以使用它来查找代码的哪一部分仍在“引用”您的对象,可以这么说。

Hope this helps.

希望这可以帮助。