Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Created January 10, 2012 13:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rednaxelafx/1588977 to your computer and use it in GitHub Desktop.
Save rednaxelafx/1588977 to your computer and use it in GitHub Desktop.
Example of HotSpot VM using the address of an object as its identity hash code, when -XX:hashCode=4 (not default)
$ jps
19144 GroovyStarter
19230 Jps
$ ps aux | grep java
sajia 19144 15.7 0.3 8621936 113804 pts/11 Sl+ 20:45 0:03 /home/sajia/sdk/jdk1.6.0_30/bin/java -XX:hashCode=4 -classpath /home/sajia/sdk/groovy-1.7.7/lib/groovy-1.7.7.jar -Dscript.name=/home/sajia/sdk/groovy-1.7.7/bin/groovysh -Dprogram.name=groovysh -Dgroovy.starter.conf=/home/sajia/sdk/groovy-1.7.7/conf/groovy-starter.conf -Dgroovy.home=/home/sajia/sdk/groovy-1.7.7 -Dtools.jar=/home/sajia/sdk/jdk1.6.0_30/lib/tools.jar org.codehaus.groovy.tools.GroovyStarter --main org.codehaus.groovy.tools.shell.Main --conf /home/sajia/sdk/groovy-1.7.7/conf/groovy-starter.conf --classpath .
sajia 19254 0.0 0.0 61176 752 pts/13 S+ 20:45 0:00 grep java
$ clhsdb
hsdb> attach 19144
Attaching to process 19144, please wait...
hsdb> inspect 0x5bde2768
hsdb> quit
$ clhsdb
hsdb> attach 19144
Attaching to process 19144, please wait...
hsdb> universe
Heap Parameters:
ParallelScavengeHeap [ PSYoungGen [ eden = [0x0000000758600000,0x000000075c332018,0x00000007603c0000] , from = [0x00000007618b0000,0x00000007618b0000,0x0000000762da0000] , to = [0x00000007603c0000,0x00000007603c0000,0x00000007618b0000] ] PSOldGen [ [0x0000000609200000,0x0000000609200000,0x000000061e140000] ] PSPermGen [ [0x0000000604000000,0x00000006050ada10,0x00000006054c0000] ] ]
hsdb> inspect 0x75bde2768
instance of Oop for java/lang/Object @ 0x000000075bde2768 @ 0x000000075bde2768 (size = 16)
_mark: 394569148417
hsdb> quit
$ java -version
java version "1.6.0_30"
Java(TM) SE Runtime Environment (build 1.6.0_30-b12)
Java HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode)
$ export JAVA_OPTS='-XX:hashCode=4'
$ groovysh
Groovy Shell (1.7.7, JVM: 1.6.0_30)
Type 'help' or '\h' for help.
----------------------------------------------------------------------------------------------------------------------------
groovy:000> o = new Object()
===> java.lang.Object@5bde2768
groovy:000> o.hashCode()
===> 1541285736
groovy:000> quit

With -XX:hashCode=4, HotSpot VM uses the address of the object when its identity hash code is first accessed, but only the lower 31 bits are used as the hashCode on LP64.

The example in this gist is:

A Groovy shell with -XX:hashCode=4 enabled, create a java.lang.Object instance in it, and its identity hash code (0x5bde2768) is shown in java.lang.Object@5bde2768; a call to its hashCode() method confirms the same value (0x5bde2768 == 1541285736). This is the lower-31 bits of the address of the object.

Without quitting the Groovy shell just yet, open up a CLHSDB (sun.jvm.hotspot.CLHSDB), and use it to see how the hashCode relates to the address of the object instance. Using the command universe, we can get the base address of the eden space, 0x0000000758600000, and then replace the lower 31-bits of this address with the hashCode we saw from the Groovy shell session, we'd get 0x75bde2768. Then use the inspect command to see what's there, and look what we've got:

hsdb> inspect 0x75bde2768
instance of Oop for java/lang/Object @ 0x000000075bde2768 @ 0x000000075bde2768 (size = 16)
_mark: 394569148417
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment