Printing unique object identity for debugging purposes in Java

16,266

I think, that actually there isn't any method (i.e. technique) which will guarantee you such uniqueness of the object's ID. The only purpose of such identification is a method for addressing to this object's data in memory. In case when this object dies and then is removed from the memory by the GC, nobody will restrict system to use its former address space for placing new a object's data.

But in fact during some short period among all available objects which have any references inside your program, System.identityHashCode(obj) will actually give you unique identity of the object. This is because this hashCode is calculated using the object's in-memory location. However, it is an implementation feature, which is not documented and not guaranteed for foreign JVMs.

Also, you can read this famous QA: Java: How to get the unique ID of an object which overrides hashCode()?

Share:
16,266
hyde
Author by

hyde

Just a dev

Updated on July 19, 2022

Comments

  • hyde
    hyde almost 2 years

    Let's start with code.

    Future<MyResult> obj = getFuture();
    debugLog.println("Handling future: " + System.identityHashCode(obj);
    

    and then somewhere else same again, or possibly the same piece of code executed again, possibly in different thread.

    Future<MyResult> obj = getFuture();
    debugLog.println("Handling future: " + System.identityHashCode(obj);
    

    Now above, if obj's are same object, the debug prints will be same, obviously. But if they are different, there's still a chance of hash collision, so the printout may be same even for different objects. So having same output (or more generally, same string) does not guarantee same object.

    Question: Is there a way in Java to get unique id string for arbitrary object? Or more formally, give static String Id.str(Object o) method so that so that this is always true:

    final Object obj1 = ...;
    final String firstId = Id.str(obj1);
    // arbitrary time passes, garbage collections happen etc.
    // firstId and obj1 are same variables as above
    (firstId.equals(Id.str.obj2)) == (obj1 == obj2)
    
  • Andremoniy
    Andremoniy over 11 years
    And how you suggest to assign such ID's from singleton to objects, which will be created by 3d-party objects?
  • codeghost
    codeghost over 11 years
    Sorry, I must have missed where he said that, I was assuming he is defining the classes and can therefore put what he likes in them.
  • hyde
    hyde over 11 years
    Well, one purpose is what is implied in the question: looking at logs and seeing printed IDs. Obviously there are workarounds, such as printing some case-specific identifying data when such is available. But when type is something like Future<>, this is hard. So I'm asking if a generic solution exists.
  • codeghost
    codeghost over 11 years
    Sorry, not meaning to extend this line, as clearly this isn't what you want, but given that Future is generic, I was inferring that it was the type of the returned result you cared about and incorrectly assumed those were going to be your objects. If you need this to work across any and all classes, especially including those you haven't produced then this is not possible and the likelihood of collision increases as you have no view of the hashcode implementations.