Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8331686

Deprecate the Memory-Access Methods in sun.misc.Unsafe for Removal

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 23
    • core-libs
    • None
    • source
    • low
    • Hide
      Compiling code with references to these methods will print warnings, which might be fatal if compiling with -Werror. `@SuppressWarnings("removal")` can be used to temporarily suppress.

      The proposed default is to allow the memory access methods be used without warning so the compatibility risk of the changes proposed in JEP 471 is low.
      Show
      Compiling code with references to these methods will print warnings, which might be fatal if compiling with -Werror. `@SuppressWarnings("removal")` can be used to temporarily suppress. The proposed default is to allow the memory access methods be used without warning so the compatibility risk of the changes proposed in JEP 471 is low.
    • Java API, add/remove/modify command line option
    • JDK

      Summary

      Deprecate the memory-access methods in sun.misc.Unsafe for removal in a future release, as proposed in JEP 471.

      Add a command line option to allow or deny usage of these methods. The default value for JDK 23 will be to allow, without warnings, so the only immediate compatibility / impact of the changes is removal warnings at compile-time.

      Problem

      VarHandles were introduced in Java 9 to safely and efficiently manipulate on-heap memory. The MemorySegment API (part of the Foreign Function & Memory API) became a permanent API in Java 22 to safely and efficiently access off-heap memory. It's time to encourage library developers that use the undocumented Unsafe API to move to the supported replacements. This is important to allow applications migrate smoothly to JDK releases in the future.

      Solution

      Deprecate the memory-access methods for removal. In total we propose to deprecate 97 methods and constants, for removal in a future release. 3 of the methods were previously deprecated in JDK 18 (JDK-8278223).

      Add the command line option --sun-misc-unsafe-memory-access=<value> to control whether to allow or deny usage of the memory access methods. This command line option can also be used to get a warning on first usage, or print a stack trace so that the code using these methods can be quickly identified.

      Specification

      Deprecate for removal all memory access methods as listed in JEP 471. A @deprecated message is added to each method and constant. Attached is the generated javadoc. The JDK does not generate or publish the javadoc for this class so it will only be seen by developers that open the source file.

      java -X will have the following in the usage output:

          --sun-misc-unsafe-memory-access=<value>
                            allow or deny usage of unsupported API sun.misc.Unsafe
                            <value> is one of "allow", "warn", "debug", or "deny".
                           The default value is "allow".

      The default value for JDK 23 will be "allow". It allows the memory access methods be used without any warning.

      The value "warn" allows the memory access methods be used and prints at most one warning to standard error when any memory methods is used. An example warning is shown in the JEP.

      The value "debug" allows the memory access methods be used and prints a message and stack trace to standard error when a memory access method is used.

      The value "deny" causes all memory access methods to throw UnsupportedOperationException.

      The man page for the "java" command is updated to document the option, see attached text.

      A release note is planned.

            alanb Alan Bateman
            rpressler Ron Pressler
            Ron Pressler
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: