Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CODETOOLS-7902944: JMH: Avoid direct weak references in Blackhole.consume(Object) #42

wants to merge 4 commits into
base: master
Choose a base branch


Copy link

@shipilev shipilev commented May 26, 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>(
at java.base/java.lang.ref.Reference.<init>(
at java.base/java.lang.ref.WeakReference.<init>(
at org.openjdk.jmh.infra.Blackhole.consumeFull(
at org.openjdk.jmh.infra.Blackhole.consume(
at org.openjdk.jmh_generated.PrimitiveClasses_test_jmhTest.test_avgt_jmhStub(
at org.openjdk.jmh_generated.PrimitiveClasses_test_jmhTest.test_AverageTime(
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(
at java.base/java.lang.reflect.Method.invoke(
at org.openjdk.jmh.runner.BenchmarkHandler$
at org.openjdk.jmh.runner.BenchmarkHandler$
at java.base/
at java.base/java.util.concurrent.Executors$
at java.base/
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.base/java.util.concurrent.ThreadPoolExecutor$
at java.base/

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.


  • Change must not contain extraneous whitespace
  • Change must be properly reviewed



Using git

Checkout this PR locally:
$ git fetch pull/42/head:pull/42
$ git checkout pull/42

Update a local copy of the PR:
$ git checkout pull/42
$ git pull pull/42/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 42

View PR using the GUI difftool:
$ git pr show -t 42

Using diff file

Download this PR as a diff file:

Copy link

@bridgekeeper bridgekeeper bot commented May 26, 2021

👋 Welcome back shade! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr label May 26, 2021
Copy link

@mlbridge mlbridge bot commented May 26, 2021


@shipilev shipilev changed the title CODETOOLS-7902944: JMH: Avoid weak references in Blackhole.consume(Object) CODETOOLS-7902944: JMH: Avoid direct weak references in Blackhole.consume(Object) May 26, 2021
Copy link
Collaborator Author

@shipilev shipilev commented May 26, 2021

This seems to have implications on performance. On hold.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
1 participant