Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-5612

Replace use of sun.misc.Unsafe#invokeCleaner with recommended replacement

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Internal
    • None
    • Java Drivers

      The com.mongodb.internal.connection.tlschannel.util.DirectBufferDeallocator.Java9Deallocator class reflectively calls the method sun.misc.Unsafe#invokeCleaner  in order to promptly free system memory encapsulated by a java.nio.DirectByteBuffer.  This code executes on all Java versions >= 9.

      As of the release of Java 23, this method in sun.misc.Unsafe is deprecated (see https://openjdk.org/jeps/471), and application authors will start using the new command line flag --sun-misc-unsafe-memory-access to warn about any use of this and other deprecated methods in the Unsafe class. Some may choose to avoid libraries that rely on any of these deprecated methods. The next step will be for the JDK to throw runtime exceptions from these methods by default. And eventually, the methods will be removed from the JDK.

      We should get ahead of this by replacing this use of sun.misc.Unsafe#invokeCleaner. The recommended replacement is this:

           * @deprecated Use a {@link MemorySegment} allocated in an {@link Arena} with the
           * appropriate temporal bounds. The {@link MemorySegment#asByteBuffer()} method
           * wraps a memory segment as a {@code ByteBuffer} to allow interop with existing
           * code.
      

      Alternatively, we can remove the deallocator entirely if we can show that it doesn't measurably affect performance.

      We should also report this to the upstream library from which this code is vendored.

            Assignee:
            Unassigned Unassigned
            Reporter:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: