Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
yogi - 11 Aug 2006 06:47 GMT
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 .
Eric Sosman - 11 Aug 2006 13:46 GMT
> Hi
>
[quoted text clipped - 4 lines]
> 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.

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Matt Humphrey - 11 Aug 2006 14:37 GMT
> Hi
>
[quoted text clipped - 4 lines]
> 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 matth@ivizNOSPAM.com http://www.iviz.com/
dsjoblom@abo.fi - 11 Aug 2006 18:06 GMT
> Hi
>
[quoted text clipped - 4 lines]
> 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
Eric Sosman - 11 Aug 2006 18:59 GMT
dsjoblom@abo.fi wrote On 08/11/06 13:06,:

>>Hi
>>
[quoted text clipped - 11 lines]
> 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.

Signature

Eric.Sosman@sun.com

dsjoblom@abo.fi - 11 Aug 2006 19:18 GMT
> dsjoblom@abo.fi wrote On 08/11/06 13:06,:
> >
[quoted text clipped - 29 lines]
>       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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.