Java SE 8 is the most reliable and stable JDK version still used by most developers. Regular CPU updates keep it safe and free of bugs, but this Maintenance Release 4 (MR4) due in October 2022 will bring some critical changes both to OpenJDK code and Java SE specification.
JDK-8202260 — issue behind the changes
JDK-8202260: Reference objects should not support cloning describes a critical issue identified in the Java SE 8 Platform. The semantics of cloning a reference object is not defined clearly in the Java SE Specification. Cloning is closely related to garbage collection, so if the reachability state of a reference object changes during GC activities, the collector may enqueue the object before the code calls the clone()
method on it. As a result, the clone won’t be enqueued and referenced. This leads to highly unpredictable reference processing. The Reference.clone()
was specified in Java SE 11 to always throw a CloneNotSupportedException.
There are two related problems:
- There was no explicit prohibition to retrieve a referent of a phantom reference between the times it was enqueued and cleared by the GC. To fix this behavior, the Java SE 9 specification was updated
- The application was allowed to add a Reference object to the registered queue before it was cleared by the GC. It was also also adjusted in the Java SE 9 specification
Now the changes introduced to Java SE 11 & 9 are backported into Java SE 8.
How the MR4 will affect the app’s code
There are two major changes that need to be introduced into the code of applications running on JDK 8:
Reference::clone()
will always throw a CloneNotSupportedException. A workaround for the code cloning referenced objects is to create a new reference object with a constructorRuntime.runFinalizersOnExit()
will throw an UnsupportedOperationException. This method was deprecated in Java 1.2 due ro reliability issues and removed in Java 9. If the code utilizesrunFinalizersOnExit()
, it has to be refactored so as to remove the invocation
Conclusion
The new OpenJDK builds will be released in October. Please update your JDK version to keep your runtime safe from vulnerabilities and introduce aforementioned changes into your Java 8 program if necessary.