让两个对象==为true

zking3 2014-12-11
让两个对象==为true


l857527302 2014-12-11
new 就是创建实例,你创建两个不同的实例,要让他们的地址相等?这。。。
pulsar_lxl 2014-12-11
这个是可能的,我实现过,
/**
        -Xmx20M
        -Xms20M
        -Xmn5M
        -XX:+PrintGCDetails
        -XX:+PrintGCDateStamps
        -XX:SurvivorRatio=8
        -verbose:gc
        -XX:+PrintHeapAtGC
        -XX:+UseConcMarkSweepGC
        -XX:MaxTenuringThreshold=0
        -XX:CMSInitiatingOccupancyFraction=10
        -XX:+UseCMSCompactAtFullCollection
        -XX:CMSFullGCsBeforeCompaction=10
        -XX:+UseCMSInitiatingOccupancyOnly
        -XX:+PrintCMSInitiationStatistics
        -XX:hashCode=4
        */
        byte[] a = new byte[3 * 1024 * 1024];
        System.out.println("------------------------>> a.hashCode : " + a.hashCode());
        a = null;
        byte[] b = new byte[3 * 1024 * 1024];
        System.out.println("------------------------>> b.hashCode : " +b.hashCode());
        b = null;
        byte[] c = new byte[3 * 1024 * 1024];
        System.out.println("------------------------>> c.hashCode : " +c.hashCode());
//        c = null;
        byte[] d = new byte[3 * 1024 * 1024];
        System.out.println("------------------------>> d.hashCode : " +d.hashCode());
        System.out.println("------------------------>> c.hashCode == d.hashCode : " + (d.hashCode()==c.hashCode()));


输出的log如下
{Heap before GC invocations=0 (full 0):
 par new generation   total 4608K, used 1475K [0x00000007f9a00000, 0x00000007f9f00000, 0x00000007f9f00000)
  eden space 4096K,  36% used [0x00000007f9a00000, 0x00000007f9b70ca8, 0x00000007f9e00000)
  from space 512K,   0% used [0x00000007f9e00000, 0x00000007f9e00000, 0x00000007f9e80000)
  to   space 512K,   0% used [0x00000007f9e80000, 0x00000007f9e80000, 0x00000007f9f00000)
 concurrent mark-sweep generation total 15360K, used 0K [0x00000007f9f00000, 0x00000007fae00000, 0x00000007fae00000)
 concurrent-mark-sweep perm gen total 21248K, used 2880K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)
2014-09-11T09:22:32.295-0800: [GC [ParNew: 1475K->0K(4608K), 0.0033740 secs] 1475K->553K(19968K), 0.0034000 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
Heap after GC invocations=1 (full 0):
 par new generation   total 4608K, used 0K [0x00000007f9a00000, 0x00000007f9f00000, 0x00000007f9f00000)
  eden space 4096K,   0% used [0x00000007f9a00000, 0x00000007f9a00000, 0x00000007f9e00000)
  from space 512K,   0% used [0x00000007f9e80000, 0x00000007f9e80000, 0x00000007f9f00000)
  to   space 512K,   0% used [0x00000007f9e00000, 0x00000007f9e00000, 0x00000007f9e80000)
 concurrent mark-sweep generation total 15360K, used 553K [0x00000007f9f00000, 0x00000007fae00000, 0x00000007fae00000)
 concurrent-mark-sweep perm gen total 21248K, used 2880K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)
}
------------------------>> a.hashCode : 2040528896
{Heap before GC invocations=1 (full 0):
 par new generation   total 4608K, used 3235K [0x00000007f9a00000, 0x00000007f9f00000, 0x00000007f9f00000)
  eden space 4096K,  79% used [0x00000007f9a00000, 0x00000007f9d28fe8, 0x00000007f9e00000)
  from space 512K,   0% used [0x00000007f9e80000, 0x00000007f9e80000, 0x00000007f9f00000)
  to   space 512K,   0% used [0x00000007f9e00000, 0x00000007f9e00000, 0x00000007f9e80000)
 concurrent mark-sweep generation total 15360K, used 553K [0x00000007f9f00000, 0x00000007fae00000, 0x00000007fae00000)
 concurrent-mark-sweep perm gen total 21248K, used 2886K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)
2014-09-11T09:22:32.300-0800: [GC [ParNew: 3235K->0K(4608K), 0.0005860 secs] 3789K->554K(19968K), 0.0006100 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap after GC invocations=2 (full 0):
 par new generation   total 4608K, used 0K [0x00000007f9a00000, 0x00000007f9f00000, 0x00000007f9f00000)
  eden space 4096K,   0% used [0x00000007f9a00000, 0x00000007f9a00000, 0x00000007f9e00000)
  from space 512K,   0% used [0x00000007f9e00000, 0x00000007f9e00000, 0x00000007f9e80000)
  to   space 512K,   0% used [0x00000007f9e80000, 0x00000007f9e80000, 0x00000007f9f00000)
 concurrent mark-sweep generation total 15360K, used 554K [0x00000007f9f00000, 0x00000007fae00000, 0x00000007fae00000)
 concurrent-mark-sweep perm gen total 21248K, used 2886K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)
}
------------------------>> b.hashCode : 2040528896
{Heap before GC invocations=2 (full 0):
 par new generation   total 4608K, used 3156K [0x00000007f9a00000, 0x00000007f9f00000, 0x00000007f9f00000)
  eden space 4096K,  77% used [0x00000007f9a00000, 0x00000007f9d15060, 0x00000007f9e00000)
  from space 512K,   0% used [0x00000007f9e00000, 0x00000007f9e00000, 0x00000007f9e80000)
  to   space 512K,   0% used [0x00000007f9e80000, 0x00000007f9e80000, 0x00000007f9f00000)
 concurrent mark-sweep generation total 15360K, used 554K [0x00000007f9f00000, 0x00000007fae00000, 0x00000007fae00000)
 concurrent-mark-sweep perm gen total 21248K, used 2888K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)
2014-09-11T09:22:32.301-0800: [GC [ParNew: 3156K->0K(4608K), 0.0003780 secs] 3711K->555K(19968K), 0.0004030 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
Heap after GC invocations=3 (full 0):
 par new generation   total 4608K, used 0K [0x00000007f9a00000, 0x00000007f9f00000, 0x00000007f9f00000)
  eden space 4096K,   0% used [0x00000007f9a00000, 0x00000007f9a00000, 0x00000007f9e00000)
  from space 512K,   0% used [0x00000007f9e80000, 0x00000007f9e80000, 0x00000007f9f00000)
  to   space 512K,   0% used [0x00000007f9e00000, 0x00000007f9e00000, 0x00000007f9e80000)
 concurrent mark-sweep generation total 15360K, used 555K [0x00000007f9f00000, 0x00000007fae00000, 0x00000007fae00000)
 concurrent-mark-sweep perm gen total 21248K, used 2888K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)
}
------------------------>> c.hashCode : 2040528896
{Heap before GC invocations=3 (full 0):
 par new generation   total 4608K, used 3127K [0x00000007f9a00000, 0x00000007f9f00000, 0x00000007f9f00000)
  eden space 4096K,  76% used [0x00000007f9a00000, 0x00000007f9d0de28, 0x00000007f9e00000)
  from space 512K,   0% used [0x00000007f9e80000, 0x00000007f9e80000, 0x00000007f9f00000)
  to   space 512K,   0% used [0x00000007f9e00000, 0x00000007f9e00000, 0x00000007f9e80000)
 concurrent mark-sweep generation total 15360K, used 555K [0x00000007f9f00000, 0x00000007fae00000, 0x00000007fae00000)
 concurrent-mark-sweep perm gen total 21248K, used 2926K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)
2014-09-11T09:22:32.302-0800: [GC [ParNew: 3127K->0K(4608K), 0.0020440 secs] 3682K->3628K(19968K), 0.0020610 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap after GC invocations=4 (full 0):
 par new generation   total 4608K, used 0K [0x00000007f9a00000, 0x00000007f9f00000, 0x00000007f9f00000)
  eden space 4096K,   0% used [0x00000007f9a00000, 0x00000007f9a00000, 0x00000007f9e00000)
  from space 512K,   0% used [0x00000007f9e00000, 0x00000007f9e00000, 0x00000007f9e80000)
  to   space 512K,   0% used [0x00000007f9e80000, 0x00000007f9e80000, 0x00000007f9f00000)
 concurrent mark-sweep generation total 15360K, used 3628K [0x00000007f9f00000, 0x00000007fae00000, 0x00000007fae00000)
 concurrent-mark-sweep perm gen total 21248K, used 2926K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)
}
2014-09-11T09:22:32.305-0800: [GC [1 CMS-initial-mark: 3628K(15360K)] 6719K(19968K), 0.0005820 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
------------------------>> d.hashCode : 2040528896
------------------------>> c.hashCode == d.hashCode : true


可以看到c.hashCode == d.hashCode : true

具体理论请参考http://hllvm.group.iteye.com/group/topic/39183#post-256300
RednaxelaFX 2014-12-11
pulsar_lxl 写道
这个是可能的,我实现过,
/**
        ...
        -XX:hashCode=4
        */

...

可以看到c.hashCode == d.hashCode : true

具体理论请参考http://hllvm.group.iteye.com/group/topic/39183#post-256300

一出动-XX:hashCode=4就已经在出猫了 
zking3 2014-12-11
-XX:hashCode=4  怎么了 R大
zking3 2014-12-11
RednaxelaFX 写道
有兴趣可以看看我以前做的一个演示,把 -XX:hashCode=4 设上之后的样子:https://gist.github.com/rednaxelafx/1588977

已经使用了某工具 确实访问不到。有能力的朋友,能不能引进来。
pulsar_lxl 2014-12-11
zking3 写道
RednaxelaFX 写道
有兴趣可以看看我以前做的一个演示,把 -XX:hashCode=4 设上之后的样子:https://gist.github.com/rednaxelafx/1588977

已经使用了某工具 确实访问不到。有能力的朋友,能不能引进来。


RednaxelaFX 写道
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
RednaxelaFX 2014-12-12
嗯我只是想强调一下:identity hash code冲突一点也不奇怪;不同时间创建的对象复用了同一个地址也不奇怪。关键是两个不同的对象一定不会在同一时间在同一地址,所以如果a与b分别引用了两个不同的对象的话,那么 (a == b) 一定是false。

我没看到楼主编辑之前原本的问题,不知道原本具体是怎么问的?
zking3 2014-12-12
最初问的就是  “有没有办法让两个对象(==)为true”   太菜了   。 


完全明白了  谢谢R大。
RednaxelaFX 2014-12-12
zking3 写道
最初问的就是  “有没有办法让两个对象(==)为true”   太菜了   。 

没事没事。刨根问底是好事。
Global site tag (gtag.js) - Google Analytics