8169425: Values computed by a ClassValue should not strongly reference the ClassValue

Paul Sandoz paul.sandoz at oracle.com
Wed May 10 18:16:30 UTC 2017


I would like to still propose this note for 9, we ain’t gonna resolve the underlying issue in 9.

It’s currently an api note but it’s more appropriate as an implementation note (e.g. Ephemerons might help).

We could amend the note with Peter’s suggestion as follows:

 Care should be taken to ensure that this ClassValue is not strongly reachable
 from the computed value, more specifically for the case when the class loader
 of the computed value’s class is not the same as or a descendent of the class
 loader of this ClassValue’s class.
 Doing so will prevent classes and their loader from being garbage collected
 which in turn may induce out of memory errors.


> On 8 Nov 2016, at 15:27, Paul Sandoz <Paul.Sandoz at oracle.com> wrote:
> Hi,
> Please review the addition of an api note to ClassValue.computeValue.
> There is some history behind this issue. Another issue was logged [1] related to Groovy using ClassValue and there being a memory leak with classes/loaders not being GC’ed, but it turned out the problem was with Groovy's explicit retention of computed values in a global set. So i closed that issue down.
> But, there is an edge case where it’s possible to induce out of memory errors with ClassValue, specifically if the computed value holds onto the corresponding ClassValue instance. I think this is an edge case and does not warrant a change to the ClassValue implementation to support weak refs to computed values which is likely to complicate an already intricate implementation and perturb its performance characteristics.
> So i have opted for an api note. I don’t want to normatively specify this, nor do i want to allude to various implementation details. (One can argue a similar note could be written for ThreadLocal.)
> Thanks,
> Paul.
> [1] https://bugs.openjdk.java.net/browse/JDK-8136353
> --- a/src/java.base/share/classes/java/lang/ClassValue.java	Tue Nov 08 12:36:21 2016 -0800
> +++ b/src/java.base/share/classes/java/lang/ClassValue.java	Tue Nov 08 15:25:04 2016 -0800
> @@ -62,6 +62,13 @@
>      * If this method throws an exception, the corresponding call to {@code get}
>      * will terminate abnormally with that exception, and no class value will be recorded.
>      *
> +     * @apiNote
> +     * Care should be taken to ensure that this {@code ClassValue} is not
> +     * <a href="../ref/package-summary.html#reachability"><em>strongly reachable</em></a>
> +     * from the computed value.  Doing so may prevent classes and their loaders
> +     * from being garbage collected which in turn may induce out of memory
> +     * errors.
> +     *
>      * @param type the type whose class value must be computed
>      * @return the newly computed value associated with this {@code ClassValue}, for the given class or interface
>      * @see #get

More information about the core-libs-dev mailing list