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?
EDIT: No problem in using profil
If you dump the heap and analyse it you get find all the references. Profilers like VisualVM and YourKit can do this for you.
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.
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.
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().
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.
Ok, Netbeans show all the references to an object.
First, run the project in debug mode CTRL + F5
, after, show the Loaded Classes Alt + Shift + 4
or Window->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 Netbeans you can use the Find Usages feature to see where a particular class may have been referenced inside of a particular project.
From the Project Explorer, select the class and right click > Find Usages.
The results looks a bit like the following image: