GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for creating direct ByteBuffers from MemorySegments by chrisvest · Pull Request #15231 · netty/netty

Conversation

@chrisvest

@chrisvest chrisvest commented May 21, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

See https://github.com/netty/netty/wiki/Java-24-and-sun.misc.Unsafe for more information.

Motivation:
Netty's performance, particularly in very busy systems, relies heavily on the ability to immediately free native memory when it's no longer used.

Since time immemorial, we have been relying on sun.misc.Unsafe, and various hacks based upon it, for quickly releasing/cleaning direct ByteBuffers.

From Java 24 onwards, any use of Unsafe has started to produce warnings, and future versions will deny access entirely. For that reason, we no longer rely on Unsafe by default (it can still be explicitly enabled) when running on Java 24 or greater.

This creates a large performance gap that needs to be closed. Fortunately, Java 22 introduced the MemorySegment and related (FFM) APIs, and it is possible to immediately release native memory using these APIs.

As Netty 4.2 is now also based on Java 8, we have access to MethodHandles, which allows to craft efficient mechanisms for calling the FFM APIs, without requiring them to be available at compile time.

Modification:

The lifetime of a memory segment is tracked by the associated Arena instance, but it is not possible to obtain the Arena from a MemorySegment, nor from any ByteBuffer created from the MemorySegment. Therefor, we need to track the lifetime of both, together. The CleanableDirectBuffer interface is introduced for this purpose, and encapsulates both a ByteBuffer and its preferred mechanism for freeing it.

The Netty Cleaner API is then extended with a method for allocating direct buffers in the form of the CleanableDirectBuffer. Putting this in the cleaner allow us to switch the buffer allocation and releasing behaviour depending on the Java version, as we already have version specific implementations.

A new CleanerJava24 implementation is added, which – as mentioned – uses method handles to create and arena, allocate a memory segment and a byte buffer from it, and then encapsulate both in a CleanableDirectBuffer instance.

A new PlatformDependent.allocateDirect(int) method is added, which returns CleanableDirectBuffer instances depending on what Cleaner implementation we end up using.

A so-called Cleaner implementation is also added for the cases where we previously used PlatformDependent.allocateDirectNoCleaner(int). This means that the optimal mechanism for allocating and releasing buffers are now abstracted behind a single common API. This also allows us to remove all the hacks and conditional code-paths that relied on PlatformDependent.hasDirectBufferNoCleanerConstructor().

Replace all instances where we allocate direct buffers, including in our native transports, with the new CleanableDirectBuffer mechanism.

Finally, deprecate all the APIs that bypass the CleanableDirectBuffer API. We cannot remove these APIs, however, as many of them are public, and it would break compatibility.

Result:
We can once again have explicit control over direct buffer lifetimes on Java 24 and greater.

Furthermore, since the FFM APIs are stable, and aligned with the long-term direction of the Java platform, we likely won't have to worry about this functionality ever breaking again.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

franz1981, linking12, and ijuma reacted with heart emoji
All reactions
  • ❤️ 3 reactions
@chrisvest
chrisvest requested a review from normanmaurer May 21, 2025 23:41
@chrisvest
chrisvest force-pushed the 4.2-alloc-direct branch from 428ea76 to 5cefc92 Compare May 21, 2025 23:44
@chrisvest

chrisvest commented May 21, 2025

Copy link
Copy Markdown
Member Author

This resolves the performance regression that #14943 causes, but only for Netty 4.2.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chrisvest
chrisvest commented May 21, 2025
View reviewed changes
Comment thread common/src/main/java/io/netty/util/internal/CleanerJava24.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest

chrisvest commented May 22, 2025

Copy link
Copy Markdown
Member Author

We need to back port the memory segment/compression fix from main as well.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest

chrisvest commented May 22, 2025

Copy link
Copy Markdown
Member Author

The sun crypto provider is facing a similar problem to the compression algorithms, with getting the address off of closable shared memory segments:

java.lang.UnsupportedOperationException: ByteBuffer derived from closeable shared sessions not supported
	at java.base/java.nio.DirectByteBuffer.address(DirectByteBuffer.java:319)
	at java.base/com.sun.crypto.provider.GaloisCounterMode$GCMEngine.overlapDetection(GaloisCounterMode.java:919)
	at java.base/com.sun.crypto.provider.GaloisCounterMode$GCMDecrypt.doFinal(GaloisCounterMode.java:1575)
	at java.base/com.sun.crypto.provider.GaloisCounterMode.engineDoFinal(GaloisCounterMode.java:442)
	at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2558)
	at java.base/sun.security.ssl.SSLCipher$T12GcmReadCipherGenerator$GcmReadCipher.decrypt(SSLCipher.java:1638)
	at java.base/sun.security.ssl.SSLEngineInputRecord.decodeInputRecord(SSLEngineInputRecord.java:239)
	at java.base/sun.security.ssl.SSLEngineInputRecord.decode(SSLEngineInputRecord.java:196)
	at java.base/sun.security.ssl.SSLEngineInputRecord.decode(SSLEngineInputRecord.java:159)
	at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:111)

/cc @AlanBateman

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented May 22, 2025

Copy link
Copy Markdown
Contributor

Do you have data points for the performance regression (which is expected eh, just curious)?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest

chrisvest commented May 22, 2025

Copy link
Copy Markdown
Member Author

@franz1981 It impacts GC behaviour. Not doing anything to deallocate works fine until it suddenly doesn't, and your application starts to have a bad time. I don't have concrete numbers to share, but it was quite significant for some services.

franz1981 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer

normanmaurer commented May 22, 2025

Copy link
Copy Markdown
Member

Also its important to call out that it affects if we allocate direct or heap buffers by default (when you call buffer(...). Without unsafe we use heap buffers when then will affect the needed heap size and also will need copy for heap to direct memory.

franz1981 reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer normanmaurer added this to the 4.2.2.Final milestone May 22, 2025
@chrisvest

chrisvest commented May 22, 2025

Copy link
Copy Markdown
Member Author

Yeah, I should put an article on the wiki about this, so people have some guidance.

normanmaurer and ijuma reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer

normanmaurer commented May 22, 2025

Copy link
Copy Markdown
Member

Yeah, I should put an article on the wiki about this, so people have some guidance.

We may also want to reconsider this in general... like if you use the pooled allocator we might still want to allocate direct by default even with no unsafe as hopefully the deallocation etc should be not as frequent

chrisvest, franz1981, and ijuma reacted with thumbs up emoji
All reactions
  • 👍 3 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlanBateman

AlanBateman commented May 22, 2025

Copy link
Copy Markdown

The sun crypto provider is facing a similar problem to the compression algorithms, with getting the address off of closable shared memory segments:

There is work in progress in JDK-8357268 to fix this, and a few others that were missed.

chrisvest reacted with thumbs up emoji franz1981 reacted with heart emoji
All reactions
  • 👍 1 reaction
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer

normanmaurer commented May 22, 2025

Copy link
Copy Markdown
Member

Yeah, I should put an article on the wiki about this, so people have some guidance.

We may also want to reconsider this in general... like if you use the pooled allocator we might still want to allocate direct by default even with no unsafe as hopefully the deallocation etc should be not as frequent

See #15232

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

normanmaurer
normanmaurer requested changes May 22, 2025
View reviewed changes

@normanmaurer normanmaurer left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First round of review

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java Outdated

@normanmaurer normanmaurer May 22, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this ? can't we access it via the cleanable ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chrisvest chrisvest May 22, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but it adds an indirection (and makes the change bigger)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@normanmaurer normanmaurer May 22, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just a bit concerned that we add just another field and add more overhead (memory).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chrisvest chrisvest May 22, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the common case yeah, this does increase the object size by 8 bytes… but we don't always have a cleanable, so then we'd have to wrap it in a noop implementation

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread common/src/main/java/io/netty/util/internal/Cleaner.java Outdated

@normanmaurer normanmaurer May 22, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as this interface is package private we can also just directly remove it

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chrisvest chrisvest May 22, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still gets used via Unpooled.wrapped(ByteBuffer). Maybe it shouldn't be deprecated.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@normanmaurer normanmaurer May 22, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this be used ? It is package-private so can't be called directly ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chrisvest chrisvest May 22, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you create a ByteBuf with Unpooled.wrapped(ByteBuffer), then we don't have a CleanableDirectBuffer instance. In that case, when releasing the ByteBuf, we go through this old code path.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@chrisvest
chrisvest force-pushed the 4.2-alloc-direct branch from dd8a9d1 to 3563f96 Compare May 22, 2025 17:32
@chrisvest

chrisvest commented May 23, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

@minborg We spotted another case for https://bugs.openjdk.org/browse/JDK-8357268 in sun.nio.ch.sctp.SctpChannelImpl:

java.lang.UnsupportedOperationException: ByteBuffer derived from closeable shared sessions not supported
	at java.base/java.nio.DirectByteBuffer.address(DirectByteBuffer.java:319)
	at jdk.sctp/sun.nio.ch.sctp.SctpChannelImpl.receiveIntoNativeBuffer(SctpChannelImpl.java:832)
	at jdk.sctp/sun.nio.ch.sctp.SctpChannelImpl.receive(SctpChannelImpl.java:806)
	at jdk.sctp/sun.nio.ch.sctp.SctpChannelImpl.receive(SctpChannelImpl.java:749)
	at jdk.sctp/sun.nio.ch.sctp.SctpChannelImpl.receive(SctpChannelImpl.java:711)
	at io.netty.channel.sctp.nio.NioSctpChannel.doReadMessages(NioSctpChannel.java:274)

Edit: Ah, I see you found it already: https://github.com/openjdk/jdk/pull/25324/files#diff-9daf1d5a1c92dc1df65dc8b63be53ca9379e7fb2fe5a4d31c2e469e05947d320

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest
chrisvest force-pushed the 4.2-alloc-direct branch from c0ed6a5 to 3d73b6f Compare May 23, 2025 18:13
normanmaurer
normanmaurer requested changes May 26, 2025
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread testsuite/src/main/java/io/netty/testsuite/transport/sctp/SctpEchoTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketEchoTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUring.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollSocketTcpMd5Test.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest
chrisvest force-pushed the 4.2-alloc-direct branch from 7af0e9a to 7e92046 Compare May 27, 2025 20:12
normanmaurer
normanmaurer reviewed May 28, 2025
View reviewed changes
Comment thread transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUring.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

normanmaurer
normanmaurer requested changes May 28, 2025
View reviewed changes
Comment thread transport-native-epoll/src/test/java/io/netty/channel/epoll/EpollSocketTcpMd5Test.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer normanmaurer mentioned this pull request May 28, 2025
Testsuite: Make testing with wrapped / direct buffers more random #15281
Merged
normanmaurer added a commit that referenced this pull request May 28, 2025
@normanmaurer
Testsuite: Make testing with wrapped / direct buffers more random
0f1cac4
Motivation:

In our testsuite we often just wrapped a byte[] and passed it to the write method. While this works it also might end up not testing often with direct buffers. We should make this a bit more random and so cover direct and heap buffers. The idea popped up when reviewing #15231

Modifications:

- Add new static helper method that will either return a wrapped buffer or a direct buffers in a random fashion
- Use this method to allocate buffers

Result:

More random testing of heap / direct buffers
chrisvest pushed a commit that referenced this pull request May 28, 2025
@normanmaurer
Testsuite: Make testing with wrapped / direct buffers more random (#1…
62117d1
…5281)

Motivation:

In our testsuite we often just wrapped a byte[] and passed it to the
write method. While this works it also might end up not testing often
with direct buffers. We should make this a bit more random and so cover
direct and heap buffers. The idea popped up when reviewing
#15231

Modifications:

- Add new static helper method that will either return a wrapped buffer
or a direct buffers in a random fashion
- Use this method to allocate buffers

Result:

More random testing of heap / direct buffers
chrisvest added 4 commits May 28, 2025 11:00
@chrisvest
Add support for creating direct ByteBuffers from MemorySegments
d692d29
Motivation:
Netty's performance, particularly in very busy systems, relies heavily on the ability to immediately free native memory when it's no longer used.

Since time immemorial, we have been relying on `sun.misc.Unsafe`, and various hacks based upon it, for quickly releasing/cleaning direct ByteBuffers.

From Java 24 onwards, any use of `Unsafe` has started to produce warnings, and future versions will deny access entirely.
For that reason, we no longer rely on `Unsafe` by default (it can still be explicitly enabled) when running on Java 24 or greater.

This creates a large performance gap that needs to be closed.
Fortunately, Java 22 introduced the `MemorySegment` and related (FFM) APIs, and it is possible to immediately release native memory using these APIs.

As Netty 4.2 is now also based on Java 8, we have access to `MethodHandles`, which allows to craft efficient mechanisms for calling the FFM APIs, without requiring them to be available at compile time.

Modification:

1.
The lifetime of a memory segment is tracked by the associated `Arena` instance, but it is not possible to obtain the `Arena` from a `MemorySegment`, nor from any `ByteBuffer` created from the `MemorySegment`.
Therefor, we need to track the lifetime of both, together.
The `CleanableDirectBuffer` interface is introduced for this purpose, and encapsulates both a `ByteBuffer` and its preferred mechanism for freeing it.

2.
The _Netty_ `Cleaner` API is then extended with a method for allocating direct buffers in the form of the `CleanableDirectBuffer`.
Putting this in the cleaner allow us to switch the buffer allocation and releasing behaviour depending on the Java version, as we already have version specific implementations.

3.
A new `CleanerJava24` implementation is added, which – as mentioned – uses method handles to create and arena, allocate a memory segment and a byte buffer from it, and then encapsulate both in a `CleanableDirectBuffer` instance.

4.
A new `PlatformDependent.allocateDirect(int)` method is added, which returns `CleanableDirectBuffer` instances depending on what `Cleaner` implementation we end up using.

5.
A so-called `Cleaner` implementation is also added for the cases where we previously used `PlatformDependent.allocateDirectNoCleaner(int)`.
This means that the optimal mechanism for allocating and releasing buffers are now abstracted behind a single common API.
This also allows us to remove all the hacks and conditional code-paths that relied on `PlatformDependent.hasDirectBufferNoCleanerConstructor()`.

6.
Replace all instances where we allocate direct buffers, including in our native transports, with the new `CleanableDirectBuffer` mechanism.

7.
Finally, deprecate all the APIs that bypass the `CleanableDirectBuffer` API.
We cannot remove these APIs, however, as many of them are public, and it would break compatibility.

Result:
We can once again have explicit control over direct buffer lifetimes on Java 24 and greater.

Furthermore, since the FFM APIs are stable, and aligned with the long-term direction of the Java platform, we likely won't have to worry about this functionality ever breaking again.
@chrisvest
Fix checkstyle issues
2f75f1d
@chrisvest
Add work-around for JDK-8357145
98371af
@chrisvest
Fix IovArray constructor calls
42f718e
chrisvest added 2 commits May 28, 2025 11:00
@chrisvest
Revert removal of UnpooledUnsafeDirectByteBuf usage
ef907e2
@chrisvest
Reverts
7addbe0
@chrisvest
chrisvest force-pushed the 4.2-alloc-direct branch from 7e92046 to 7addbe0 Compare May 28, 2025 18:06
chrisvest added 3 commits May 28, 2025 11:08
@chrisvest
Use 'randomBufferType'
97f5ae0
@chrisvest
Move unpooled memory accounting into DecrementingCleanableDirectBuffer
a7e30df
@chrisvest
Correctly set limits for copy-buffers in SCTP
43dfb10
normanmaurer
normanmaurer reviewed May 30, 2025
View reviewed changes
Comment thread transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

normanmaurer
normanmaurer requested changes May 30, 2025
View reviewed changes

@normanmaurer normanmaurer left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread transport-sctp/src/main/java/io/netty/channel/sctp/oio/OioSctpChannel.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest
Update javadoc comments in SCTP
f54b5ea
chrisvest added a commit to chrisvest/netty that referenced this pull request May 30, 2025
@chrisvest
Re-enable sun.misc.Unsafe by default on Java 24+
0e76286
Motivation:
In netty#14943 we disabled Unsafe by default on Java 24+, to avoid producing warnings from the JVM.
However, disabling the use of Unsafe also disables the hacks we use to access `ByteBuffer` cleaners to eagerly deallocate them.
This can have dire performance consequences, particularly for busy applications.

Disabling the warnings from Unsafe also did nothing to stem the warnings produced by our JNI code, so many systems still see warnings anyway.

We should instead revert the changed default, and just tolerate the warning in Netty 4.1.

In Netty 4.2 we have netty#15231 as a long-term fix, where we avoid using Unsafe but will have a way to eagerly release the memory of our native ByteBuffer instances.

Modification:
Change the default for our Unsafe usage to no longer be disabled on Java 24+.

Result:
More consistent performance across Java versions.
@chrisvest chrisvest mentioned this pull request May 30, 2025
Re-enable sun.misc.Unsafe by default on Java 24+ #15296
Merged
@normanmaurer
Merge branch '4.2' into 4.2-alloc-direct
d71f7f8
normanmaurer
normanmaurer approved these changes May 30, 2025
View reviewed changes

@normanmaurer normanmaurer left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ship it! Great work

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

franz1981 reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@chrisvest
chrisvest merged commit 4dc0b3a into netty:4.2 May 30, 2025
@chrisvest
chrisvest deleted the 4.2-alloc-direct branch May 30, 2025 20:35
chrisvest added a commit that referenced this pull request May 30, 2025
@chrisvest
Re-enable sun.misc.Unsafe by default on Java 24+ (#15296)
7d6b939
See https://github.com/netty/netty/wiki/Java-24-and-sun.misc.Unsafe for
more information.

Motivation:
In #14943 we disabled Unsafe by
default on Java 24+, to avoid producing warnings from the JVM. However,
disabling the use of Unsafe also disables the hacks we use to access
`ByteBuffer` cleaners to eagerly deallocate them. This can have dire
performance consequences, particularly for busy applications.

Disabling the warnings from Unsafe also did nothing to stem the warnings
produced by our JNI code, so many systems still see warnings anyway.

We should instead revert the changed default, and just tolerate the
warning in Netty 4.1.

In Netty 4.2 we have #15231 as a
long-term fix, where we avoid using Unsafe but will have a way to
eagerly release the memory of our native ByteBuffer instances.

Modification:
Change the default for our Unsafe usage to no longer be disabled on Java
24+.

Result:
More consistent performance across Java versions.
@franz1981
franz1981 self-requested a review May 30, 2025 20:51
franz1981
franz1981 reviewed May 30, 2025
View reviewed changes

@franz1981 franz1981 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread common/src/main/java/io/netty/util/internal/CleanableDirectBuffer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Janmm14 Janmm14 mentioned this pull request Jun 3, 2025
Expose MemorySegment-backed ByteBuf memoryAddress #15309
Closed
@wendigo wendigo mentioned this pull request Jun 6, 2025
Cassandra client fails with Netty 4.2.2 #15324
Closed
@derklaro derklaro mentioned this pull request Jun 20, 2025
direct buffer preference should not depend on unsafe presence #15381
Merged
chrisvest pushed a commit that referenced this pull request Jun 20, 2025
@derklaro
direct buffer preference should not depend on unsafe presence (#15381)
23d30c7
Motivation:
Currently, the preference if a direct buffer should be allocated by, for
example, `ByteBufAllocator.buffer()` or `ByteBufAllocator.ioBuffer()`
depends on the fact if unsafe is available. It seems like this was
originally introduced as only with unsafe it was possible to get the
cleaner of a ByteBuffer to clean it. However, with the changes
introduced in #15231, it doesn't make sense anymore to depend on this as
unsafe could be unavailable but MemorySegments will still be freeable.

Modification:
Switch the check for unsafe to a check that validates if the current
cleaner implementation is able to free the allocated buffers (each
implementation can do this except the no-op one). This ensures that
direct buffers are actually preferred when they're backed by something
cleanable and still unpreferred when unsafe is unavailable and buffers
cannot be freed in a controlled way.

Result:
Direct buffers will be preferred when they're cleanable in a controlled
way unrelated to the fact if unsafe is available.
normanmaurer added a commit that referenced this pull request Jun 23, 2025
@normanmaurer
Testsuite: Make testing with wrapped / direct buffers more random (#1…
1927074
…5281)

Motivation:

In our testsuite we often just wrapped a byte[] and passed it to the
write method. While this works it also might end up not testing often
with direct buffers. We should make this a bit more random and so cover
direct and heap buffers. The idea popped up when reviewing
#15231

Modifications:

- Add new static helper method that will either return a wrapped buffer
or a direct buffers in a random fashion
- Use this method to allocate buffers

Result:

More random testing of heap / direct buffers
@normanmaurer normanmaurer mentioned this pull request Jun 23, 2025
Testsuite: Make testing with wrapped / direct buffers more random (#1… #15395
Merged
normanmaurer added a commit that referenced this pull request Jun 24, 2025
@normanmaurer
Testsuite: Make testing with wrapped / direct buffers more random (#1… (
171b33d
#15395)

…5281)

Motivation:

In our testsuite we often just wrapped a byte[] and passed it to the
write method. While this works it also might end up not testing often
with direct buffers. We should make this a bit more random and so cover
direct and heap buffers. The idea popped up when reviewing
#15231

Modifications:

- Add new static helper method that will either return a wrapped buffer
or a direct buffers in a random fashion
- Use this method to allocate buffers

Result:

More random testing of heap / direct buffers
@patrick96 patrick96 mentioned this pull request Jul 15, 2025
NullPointerException in PlatformDependent0.safeConstructPutInt with Netty 4.2.2 and GraalVM Native Image on JDK25+ #15493
Closed
@kannanjgithub kannanjgithub mentioned this pull request Jan 19, 2026
Java 25 + grpc-netty-shaded 1.78.0 appearance warning grpc/grpc-java#12612
Open
@dongjoon-hyun dongjoon-hyun mentioned this pull request Mar 26, 2026
[SPARK-56229] Remove io.netty.noUnsafe from default JVM args according to Netty change apache/spark-kubernetes-operator#593
Closed
dongjoon-hyun added a commit to apache/spark-kubernetes-operator that referenced this pull request Mar 26, 2026
@dongjoon-hyun
[SPARK-56229] Remove io.netty.noUnsafe from default JVM args accord…
4ed9b12
…ing to Netty change

### What changes were proposed in this pull request?

This PR removes `-Dio.netty.noUnsafe=true` from the default JVM args in `values.yaml`.

### Why are the changes needed?

Netty 4.2.2+ have a correct implementation instead of `Unsafe` usage for Java 25+ like the following instead of the legacy `Unsafe` operation.

- netty/netty#15231
- netty/netty#15338

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Pass the CIs with the existing tests.

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (claude-4-opus)

Closes #593 from dongjoon-hyun/SPARK-56229.

Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
@franz1981 franz1981 mentioned this pull request Nov 19, 2026
-Dio.netty.maxDirectMemory=0 leaves direct memory unlimited on Java 25 #18481
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@franz1981 franz1981 franz1981 left review comments
@normanmaurer normanmaurer normanmaurer approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.2.2.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@chrisvest @franz1981 @normanmaurer @AlanBateman
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.