RFR: CODETOOLS-7902944: JMH: Avoid direct weak references in Blackhole.consume(Object) [v3]

Aleksey Shipilev shade at openjdk.java.net
Wed May 26 15:09:41 UTC 2021


> CODETOOLS-7901867 wrapped the stored object reference with a `WeakReference`. This unfortunately fails with inline type boxes that throw the exceptions like:
> 
> 
> java.lang.IllegalArgumentException: cannot reference a primitive type: org.openjdk.PrimitiveClasses$Value
> at java.base/java.lang.ref.Reference.<init>(Reference.java:495)
> at java.base/java.lang.ref.Reference.<init>(Reference.java:489)
> at java.base/java.lang.ref.WeakReference.<init>(WeakReference.java:62)
> at org.openjdk.jmh.infra.Blackhole.consumeFull(Blackhole.java:515)
> at org.openjdk.jmh.infra.Blackhole.consume(Blackhole.java:315)
> at org.openjdk.jmh_generated.PrimitiveClasses_test_jmhTest.test_avgt_jmhStub(PrimitiveClasses_test_jmhTest.java:190)
> at org.openjdk.jmh_generated.PrimitiveClasses_test_jmhTest.test_AverageTime(PrimitiveClasses_test_jmhTest.java:152)
> at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
> at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.base/java.lang.reflect.Method.invoke(Method.java:568)
> at org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:470)
> at org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:453)
> at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
> at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
> at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
> at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
> at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
> at java.base/java.lang.Thread.run(Thread.java:831)
> 
> 
> Wrapping in `WeakReference` is not very reliable, but it is an important defense-in-depth strategy: if it hoards a large object, then a GC in the middle of benchmark would drop it. To handle inline types, it might make some sense to wrap the instance with another holder.

Aleksey Shipilev has updated the pull request incrementally with two additional commits since the last revision:

 - Rename Holder -> Box
 - Update comments

-------------

Changes:
  - all: https://git.openjdk.java.net/jmh/pull/42/files
  - new: https://git.openjdk.java.net/jmh/pull/42/files/7011be4f..eece6dfa

Webrevs:
 - full: https://webrevs.openjdk.java.net/?repo=jmh&pr=42&range=02
 - incr: https://webrevs.openjdk.java.net/?repo=jmh&pr=42&range=01-02

  Stats: 5 lines in 1 file changed: 0 ins; 0 del; 5 mod
  Patch: https://git.openjdk.java.net/jmh/pull/42.diff
  Fetch: git fetch https://git.openjdk.java.net/jmh pull/42/head:pull/42

PR: https://git.openjdk.java.net/jmh/pull/42


More information about the jmh-dev mailing list