How do i get a reference count of a object in java ?

Y

yogi

Hi

I have a question related to GC ...

How do i get a reference count of a object in java ?
Suppose i have a Object called MyObject .. i just want to know
programattically , that how many objects are referencing this objcet in
a VM ?


Thanks in Advance .
 
E

Eric Sosman

yogi said:
Hi

I have a question related to GC ...

How do i get a reference count of a object in java ?
Suppose i have a Object called MyObject .. i just want to know
programattically , that how many objects are referencing this objcet in
a VM ?

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

Matt Humphrey

yogi said:
Hi

I have a question related to GC ...

How do i get a reference count of a object in java ?
Suppose i have a Object called MyObject .. i just want to know
programattically , that how many objects are referencing this objcet in
a VM ?

Modern garbage collectors do not use reference counting. The way to find
which objects reference another is to either keep track of it yourself as
part of your design, or include a traversal method to find the objects.
There are also some sophisticated things you can do with SoftReferences,
WeakReferences, etc. Take a look at
http://www-128.ibm.com/developerworks/library/j-refs/

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
D

dsjoblom

yogi said:
Hi

I have a question related to GC ...

How do i get a reference count of a object in java ?
Suppose i have a Object called MyObject .. i just want to know
programattically , that how many objects are referencing this objcet in
a VM ?

Despite what others have said, yes it is possible:

http://java.sun.com/j2se/1.5.0/docs/guide/jvmti/jvmti.html#Heap

If you really need this is another matter. It is probably only useful
if you are writing a debugger or profiler.

Regards,
Daniel Sjöblom
 
E

Eric Sosman

Despite what others have said, yes it is possible:

http://java.sun.com/j2se/1.5.0/docs/guide/jvmti/jvmti.html#Heap

If you really need this is another matter. It is probably only useful
if you are writing a debugger or profiler.

Well, you learn something new every day!

Still, the JVMTI stuff doesn't seem to provide a way
to retrieve an object's reference count, since Java still
isn't keeping track of such a thing. The machinery could
be used to compute reference counts on demand (using
IterateOverReachableObjects or one of its brethren, along
with GetTag and SetTag), but two drawbacks seem worthy
of note:

- Object manipulation comes to a halt during the process:
no objects are created, garbage collected, or changed
in any way during the iteration. This implies that the
results must be returned after the fact, by which time
they could be out of date.

- "JVMTI may not be available in all implementations of
the Java[tm] virtual machine."

Nonetheless, thanks for the reference! I've got know
immediate use for it, but it's worth knowing the capabilities
are there should the need arise.
 
D

dsjoblom

Eric said:
Well, you learn something new every day!

Still, the JVMTI stuff doesn't seem to provide a way
to retrieve an object's reference count, since Java still
isn't keeping track of such a thing. The machinery could
be used to compute reference counts on demand (using
IterateOverReachableObjects or one of its brethren, along
with GetTag and SetTag), but two drawbacks seem worthy
of note:

- Object manipulation comes to a halt during the process:
no objects are created, garbage collected, or changed
in any way during the iteration. This implies that the
results must be returned after the fact, by which time
they could be out of date.

True, it requires a fair bit of programming, and it isn't perfect. I
should also add that it is probably not a good idea to expect the JVMTI
implementation to have lightning speed performance for this particular
task (although who knows, it does have a low overhead for other things,
I've used it for bytecode rewriting and JIT compiled code disassembly).
- "JVMTI may not be available in all implementations of
the Java[tm] virtual machine."

Also true, but JVMTI is definitely a step in the right direction (as
opposed to previous similar attempts by Sun), since it is a
standardized interface that can be, and is supposed to be implemented
by any VM vendor. The drawback right now is that it is a fairly new
interface, introduced in 1.5.

Regards,
Daniel Sjöblom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top