Why is it not supported to create References for value objects?

Glavo zjx001202 at gmail.com
Fri Dec 12 15:02:23 UTC 2025


Hi,

In the current draft of JEP 401, I saw the following statement:

> The garbage collection APIs in java.lang.ref and java.util.WeakHashMap do
not allow developers to manually manage value objects in the heap.
> Attempts to create Reference objects for value objects throw
IdentityException at run time.

We could clearly have pretended that all value objects exist forever and
are never collected, so that Reference or WeakHashMap would still work with
value objects.
Obviously, doing so would break far fewer existing codes, whereas having
them directly throw IdentityException would break a lot more code.

As an analogy, I think this is very similar to ThreadLocal on virtual
threads. Although virtual threads make many use cases of ThreadLocal
low-performance and high-memory,
Project Loom did not outright forbid users from using ThreadLocal on
virtual threads, yet Project Valhalla chose to break this (admittedly
inefficient) writing style.

I don’t understand why Project Valhalla made this choice. Has there been
any related discussion in the past? I’m really eager to know the reasoning
behind this decision.

Glavo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/valhalla-dev/attachments/20251212/c564bc33/attachment-0001.htm>


More information about the valhalla-dev mailing list