10

Is it possible and/or easy to find out how many incoming references an arbitrary object has? That is, how many objects are referring to it.

Thanks in advance...

Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135

4 Answers4

6

The short answer is: Count them yourself.

This other StackOverflow question has some useful answers and resources

Is it possible to get the object reference count?

Some more googling turns up this page: http://www.velocityreviews.com/forums/t363649-how-do-i-get-a-reference-count-of-a-object-in-java.html

Which suggests the following:

Java doesn't maintain reference counts. A particular JVM might (I haven't heard of any that does), but if a JVM maintains reference counts that's "a private matter" internal to the JVM's implementation. Java has no construct or utility class to retrieve reference counts, even if the JVM happens to maintain them. (Similarly, the JVM might maintain counters associated with its JIT compiler, but those statistics are not accessible to Java as such.)

If you want to maintain reference counts for your own objects, you'll need to do it manually. It's likely to be an error-prone business:

MyObject ref = null;
try {
    // get a reference, increment counter
    ref = MyObject.getReference(args);
    ...
} finally {
     // decrement counter, promising to drop reference
     if (ref != null) {
         ref.release();
         ref = null;
     }
}

You could establish a self-enforced "protocol" that requires this sort of boilerplate around every use of a MyObject, but it would be clumsy and all too easy to get wrong. And not all situations are as simple as the one above: For example, if you have two references whose lifetimes overlap but don't nest, you can't match them to the lexical scopes of try/finally so neatly.

The collections framework would be pretty much off-limits, or at least very hard to use. When you put a MyObject into a SortedMap, for example, how many references to the MyObject are retained in the Map? Does the answer depend on whether the MyObject is used as key or as value, or as both? It seems to me that your chances of maintaining an accurate count are slim.

Take a step back: WHY do you want these reference counts? What purpose are you trying to fulfil? There may be a better way to achieve your overall strategy than to pursue this very difficult tactic.

Community
  • 1
  • 1
AndyG
  • 39,700
  • 8
  • 109
  • 143
  • Reference counts couldn't be used for *arbitrary* objects, only for non-cyclic objects. Some reference counting systems (browsers with DOM nodes) do identify cycles between reference counted objects to free memory, but those mean that the reference count would only be an upper bound on the count. – Mike Samuel Jan 28 '14 at 05:10
3

If you really need this information, trigger a javacore dump and then analyze the information contained in it. But unless you have a specific problem that you're trying to resolve such as a "memory leak", you probably don't need this information.

keshlam
  • 7,931
  • 2
  • 19
  • 33
0

The question could only be answered by a VM that has a non-generational stop-the-world garbage collector. The JVM does not require such a garbage collector, so, no, it is not possible.

Even with such a GC, different threads are allowed to keep copies of non-volatile fields. That could could confuse the number of references that you would find if walking the live object graph looking for a particular object.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

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

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

Arindam Das
  • 206
  • 1
  • 7
  • 29