1

Let's say I have this line from a thread dump:

waiting to lock <0x0000000301cf21a8> (a foo.bar.MyClass)

How can I find this object? I thought calling System.identityHashCode and converting this to hex will do, but it doesn't seem to match.

duduamar
  • 3,816
  • 7
  • 35
  • 54
  • From the looks of it, this looks like a 64 bit number rather than 32, so it's definitely not a hash code. It could be a memory address or some other internal 64-bit identifier. – RealSkeptic Oct 04 '15 at 06:46
  • Yes, but any idea how, given an object, I can get this identifier? I tried googling for thread dump analysis, but didn't find this information – duduamar Oct 04 '15 at 07:06
  • 1
    First, you can get addresses of objects by reading this: http://stackoverflow.com/questions/8820164/is-there-a-way-to-get-a-reference-address – guillaume girod-vitouchkina Oct 04 '15 at 11:16
  • Thanks @guillaumegirod-vitouchkina - very useful indeed, this is exactly what I wanted! – duduamar Oct 06 '15 at 12:15

2 Answers2

4

Note that the number in your threaddump line is the virtual memory address, no a hash code.

You can do this by examining the JVM with a memory analyzer like Eclipse MAT.

I took an example from Oracle demonstrating a deadlock situation, created a threaddump using jstack and a heapdump using Mat.

"Thread-1" prio=10 tid=0x00007fb0a00bc000 nid=0x5cc4 waiting for monitor entry [0x00007fb098b25000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at net.noorg.playground.Deadlock$Friend.bowBack(Deadlock.java:19)
    - waiting to lock <0x00000000ebd9f320> (a net.noorg.playground.Deadlock$Friend)
    at net.noorg.playground.Deadlock$Friend.bow(Deadlock.java:16)
    - locked <0x00000000ebd9f368> (a net.noorg.playground.Deadlock$Friend)
    at net.noorg.playground.Deadlock$2.run(Deadlock.java:34)
    at java.lang.Thread.run(Thread.java:745)

Both parties are

- waiting to lock <0x00000000ebd9f320> (a net.noorg.playground.Deadlock$Friend)

and

- locked <0x00000000ebd9f368> (a net.noorg.playground.Deadlock$Friend)

In Mat create the heapdump: File > Aquire heapdump > select a process > Finish

Open the Histogram:

How to open the histogram view

In the histogram view, you can filter for the class name using a Regular Expression. Then you can show outgoing object references for that class:

Histogram View filtered by class name

In the reference list you'll find the object(s) referencing that class:

Outgoing object references of that class

In case of that example we can see the name references (String objects) and their respective values Alphonse and Gaston. So "Alphonse" and "Gaston" are the related parties in this deadlock.

Note,

  • that taking a heapdump may take serveral minutes depending on the heap size and system performance
  • that during the dump process, the JVM is paused. This may cause timeouts of established network connections or transactions.
  • and the heapdump will contain sensitive information like database passwords, customer passwords, private keys, user data etc. So you should handle the dump with care (not transferring it via insecure connections or storing it unencrypted)!

On a server you can create a heapdump using jmap (either as root or the user running the process):

$ jmap -dump:file=heapdump.hprof <PID>
try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
1

I have used @guillaumegirod-vitouchkina suggestion - I created a small code which prints the memory address using Unsafe, and then I can compare it to what I see in the thread dump. Thanks!

duduamar
  • 3,816
  • 7
  • 35
  • 54