Skip to content

Disable sun.misc.Unsafe by default on Java 24+ - #14943

Merged
normanmaurer merged 1 commit into
netty:4.1from
chrisvest:4.1-j24-unsafe
Mar 29, 2025
Merged

normanmaurer merged 1 commit into
netty:4.1from
chrisvest:4.1-j24-unsafe

Conversation

@chrisvest

@chrisvest chrisvest commented Mar 19, 2025 •

Copy link
Copy Markdown
Member

Warning

Without access to sun.misc.Unsafe, Netty is unable to eagerly free direct ByteBuffer instances, and instead has to rely on the garbage collector and reference processing.

This delays the freeing of unused memory, and can cause greatly increase the memory usage of busy systems.

To revert the Netty behavior and permit it to eagerly free memory, apply one of the following settings to your JVM command line:

  • --sun-misc-unsafe-memory-access=allow (preferred)
  • -Dio.netty.noUnsafe=false (may or may not work for shaded libraries)

Alternatively, upgrade to Netty 4.2 where we are able to apply a long-term work-around.

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


Motivation:
Java 24 has started to print warnings when sub.misc.Unsafe is used to access memory. See JEP 498: https://openjdk.org/jeps/498

People tend to be sensitive to new warnings and errors printed by the JVM, so we should avoid enabling Unsafe by default on Java 24 onwards.

Modification:
Effectively change the default value of the io.netty.noUnsafe system property for Java 24 onwards, but in a way where we can tell if it was explicitly set to a given value or if it follows the default.

Any sort of explicit settings, be it -Dio.netty.noUnsafe=false or --sun-misc-unsafe-memory-access=allow, will continue to work as before.

Manually verified that the changes work as intended on Java 24.

Result:
No more warnings printed to the console on Java 24+.

Fixes #14942

@chrisvest
chrisvest requested a review from normanmaurer March 19, 2025 21:15
Motivation:
Java 24 has started to print warnings when `sub.misc.Unsafe` is used to access memory.
See JEP 498: https://openjdk.org/jeps/498

People tend to be sensitive to new warnings and errors printed by the JVM, so we should avoid enabling `Unsafe` by default on Java 24 onwards.

Modification:
Effectively change the default value of the `io.netty.noUnsafe` system property for Java 24 onwards, but in a way where we can tell if it was explicitly set to a given value or if it follows the default.

Any sort of explicit settings, be it `-Dio.netty.noUnsafe=false` or `--sun-misc-unsafe-memory-access=allow`, will continue to work as before.

Manually verified that the changes work as intended on Java 24.

Result:
No more warnings printed to the console on Java 24+.

Fixes netty#14942
@franz1981

Copy link
Copy Markdown
Contributor

Since not using Unsafe has some relevant performance effect, switching its usage off by default isn't too much?

@hyperxpro

hyperxpro commented Mar 20, 2025 •

Copy link
Copy Markdown
Contributor

Since not using Unsafe has some relevant performance effect, switching its usage off by default isn't too much?

And if it does. We should log at INFO so people know why performance is degraded. So we don't have a flood of these issues.

@chrisvest

Copy link
Copy Markdown
Member Author

Since not using Unsafe has some relevant performance effect, switching its usage off by default isn't too much?

We won't have much choice in future java versions. For the time being, the command line flag allow people to keep using Unsafe.

We should log at INFO so people know why performance is degraded. So we don't have a flood of these issues.

Many teams are more sensitive to new log messages than they are to performance. If people investigate a change in performance after upgrading their Java version, they might notice the debug log message explaining the changed default.

@normanmaurer
normanmaurer merged commit a785e5b into netty:4.1 Mar 29, 2025
normanmaurer pushed a commit that referenced this pull request Mar 29, 2025
Motivation:
Java 24 has started to print warnings when `sub.misc.Unsafe` is used to
access memory. See JEP 498: https://openjdk.org/jeps/498

People tend to be sensitive to new warnings and errors printed by the
JVM, so we should avoid enabling `Unsafe` by default on Java 24 onwards.

Modification:
Effectively change the default value of the `io.netty.noUnsafe` system
property for Java 24 onwards, but in a way where we can tell if it was
explicitly set to a given value or if it follows the default.

Any sort of explicit settings, be it `-Dio.netty.noUnsafe=false` or
`--sun-misc-unsafe-memory-access=allow`, will continue to work as
before.

Manually verified that the changes work as intended on Java 24.

Result:
No more warnings printed to the console on Java 24+.

Fixes #14942
@re-thc

re-thc commented Mar 29, 2025

Copy link
Copy Markdown

We won't have much choice in future java versions.

JEP-498 mentions replacement standard APIs. Do those have nothing close that can give similar performance? Is it a matter of adapting the code?

@chrisvest
chrisvest deleted the 4.1-j24-unsafe branch March 29, 2025 14:37
@chrisvest

Copy link
Copy Markdown
Member Author

It's code adaptation. If we were to use the Foreign Function and Memory APIs, we'd have to make a lot of changes to support it, as the APIs are very far from how you use Unsafe. And FFM is only available from Java 22. We will instead be making those changes in the main branch.

chrisvest added a commit that referenced this pull request Mar 29, 2025
Motivation:
Java 24 has started to print warnings when `sub.misc.Unsafe` is used to
access memory. See JEP 498: https://openjdk.org/jeps/498

People tend to be sensitive to new warnings and errors printed by the
JVM, so we should avoid enabling `Unsafe` by default on Java 24 onwards.

Modification:
Effectively change the default value of the `io.netty.noUnsafe` system
property for Java 24 onwards, but in a way where we can tell if it was
explicitly set to a given value or if it follows the default.

Any sort of explicit settings, be it `-Dio.netty.noUnsafe=false` or
`--sun-misc-unsafe-memory-access=allow`, will continue to work as
before.

Manually verified that the changes work as intended on Java 24.

Result:
No more warnings printed to the console on Java 24+.

Fixes #14942

Co-authored-by: Chris Vest <[email protected]>
mfussenegger added a commit to crate/crate that referenced this pull request Apr 7, 2025
Netty uses Unsafe in some cases which causes warnings in JDK24
See https://openjdk.org/jeps/498

For now continue to use it due to performance impacts.
See discussion in netty/netty#14943
mfussenegger added a commit to crate/crate that referenced this pull request Apr 7, 2025
Netty uses Unsafe in some cases which causes warnings in JDK24
See https://openjdk.org/jeps/498

For now continue to use it due to performance impacts.
See discussion in netty/netty#14943
mfussenegger added a commit to crate/crate that referenced this pull request Apr 8, 2025
Netty uses Unsafe in some cases which causes warnings in JDK24
See https://openjdk.org/jeps/498

For now continue to use it due to performance impacts.
See discussion in netty/netty#14943
yaooqinn pushed a commit to apache/spark that referenced this pull request Apr 29, 2025
### What changes were proposed in this pull request?

This PR aims to Upgrade Netty to 4.1.121.Final.

### Why are the changes needed?

To support Java 24+ more easily and bring the latest bug fixes.
- https://netty.io/news/2025/04/23/4-1-120-Final.html
  - netty/netty#14943
  - netty/netty#15035
- https://netty.io/news/2025/04/24/4-1-121-Final.html

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

No.

### How was this patch tested?

Pass the CIs.

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

No.

Closes #50749 from dongjoon-hyun/SPARK-51944.

Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Kent Yao <[email protected]>
chrisvest added a commit to chrisvest/netty that referenced this pull request May 30, 2025
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 added a commit that referenced this pull request May 30, 2025
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.
@chrisvest

Copy link
Copy Markdown
Member Author

For people following this issue, we reverted this change in #15296 because the performance regression was too much.

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

@cdprete cdprete mentioned this pull request Sep 19, 2025
dongjoon-hyun added a commit to apache/spark-kubernetes-operator that referenced this pull request Oct 9, 2025
…ings

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

This PR aims to set `io.netty.noUnsafe` to `true` to avoid JEP-498 warnings.

### Why are the changes needed?

Java 24 has started to print warnings when `sub.misc.Unsafe` is used to access memory.
- https://openjdk.org/jeps/498

Since Netty 4.1.120.Final, we can use `io.netty.noUnsafe` setting.
- netty/netty#14943

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

No behavior change.

### How was this patch tested?

Manually tests.

**BEFORE**

```
Starting Operator...
WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::allocateMemory has been called by io.netty.util.internal.PlatformDependent0$2 (file:/opt/spark-operator/operator/spark-kubernetes-operator.jar)
WARNING: Please consider reporting this to the maintainers of class io.netty.util.internal.PlatformDependent0$2
WARNING: sun.misc.Unsafe::allocateMemory will be removed in a future release
25/10/09 03:35:46 INFO   o.a.s.k.o.SparkOperator Configuring operator with 50 reconciliation threads.
```

**AFTER**

```
Starting Operator...
25/10/09 03:32:21 INFO   o.a.s.k.o.SparkOperator Configuring operator with 50 reconciliation threads.
```

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

No.

Closes #378 from dongjoon-hyun/SPARK-53851.

Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
prabhjyotsingh pushed a commit to acceldata-io/spark3 that referenced this pull request Sep 10, 2026
prabhjyotsingh pushed a commit to acceldata-io/spark3 that referenced this pull request Sep 11, 2026
prabhjyotsingh pushed a commit to acceldata-io/spark3 that referenced this pull request Sep 11, 2026
Ko-dany pushed a commit to acceldata-io/spark3 that referenced this pull request Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PlatformDependent0: A terminally deprecated method in sun.misc.Unsafe has been called

5 participants