Skip to content

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

Merged
chrisvest merged 1 commit into
4.2from
unsafe
Mar 29, 2025
Merged

chrisvest merged 1 commit into
4.2from
unsafe

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

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

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
@normanmaurer normanmaurer added this to the 4.2.0.RC5 milestone Mar 29, 2025
@chrisvest
chrisvest merged commit 39631f5 into 4.2 Mar 29, 2025
@chrisvest
chrisvest deleted the unsafe branch March 29, 2025 14:44
@Spasi

Spasi commented Apr 24, 2025

Copy link
Copy Markdown
Contributor

Hey @normanmaurer,

This commit doesn't work as expected in 4.2, because explicitNoUnsafeCause0() depends on JAVA_VERSION, which is initialized after EXPLICIT_NO_UNSAFE_CAUSE (the call to javaVersion() always returns 0). In the original PR (#14943) for the 4.1 branch, this is taken care of by moving the EXPLICIT_NO_UNSAFE_CAUSE declaration after JAVA_VERSION, which this PR doesn't do.

Not a bug, but you may also want to flip JAVA_VERSION and IS_ANDROID and make javaVersion0() call isAndroid() instead of isAndroid0(). This will ensure that isAndroid0() is called only once, since it has a visible side-effect when debug logging is enabled.

@normanmaurer

Copy link
Copy Markdown
Member Author

@Spasi want to do a PR to fix it ?

Spasi added a commit to Spasi/netty that referenced this pull request Apr 24, 2025
Motivation:

After netty#14975, EXPLICIT_NO_UNSAFE_CAUSE has a dependency to JAVA_VERSION,
which is initialized after it. When explicitNoUnsafeCause0() tries to
set io.netty.noUnsafe to true by default on Java 24, it fails, because
javaVersion() always returns 0 at that point.

Also, isAndroid0() is called twice, once in javaVersion0() and once to
initialize IS_ANDROID. This is not a bug or a performance issue, but it
does have a visible side-effect when debug logging is enabled.

Modification:

The declaration of EXPLICIT_NO_UNSAFE_CAUSE is moved after JAVA_VERSION,
ensuring javaVersion() returns the correct version when
explicitNoUnsafeCause0() calls it.

IS_ANDROID is also moved before JAVA_VERSION, such that the isAndroid0()
method is called only once.

Result:

Fixes netty#14942 correctly
normanmaurer pushed a commit that referenced this pull request Apr 24, 2025
Motivation:

After #14975, `EXPLICIT_NO_UNSAFE_CAUSE` has a dependency to
`JAVA_VERSION`, which is initialized after it. When
`explicitNoUnsafeCause0()` tries to set `io.netty.noUnsafe` to true by
default on Java 24, it fails, because `javaVersion()` always returns 0
at that point.

Also, `isAndroid0()` is called twice, once in `javaVersion0()` and once
to initialize `IS_ANDROID`. This is not a bug or a performance issue,
but it does have a visible side-effect when debug logging is enabled.

Modification:

The declaration of `EXPLICIT_NO_UNSAFE_CAUSE` is moved after
`JAVA_VERSION`, ensuring `javaVersion()` returns the correct version
when `explicitNoUnsafeCause0()` calls it.

`IS_ANDROID` is also moved before `JAVA_VERSION`, such that the
`isAndroid0()` method is called only once.

Result:

Fixes #14942 correctly
chrisvest pushed a commit to chrisvest/netty that referenced this pull request Apr 25, 2025
Motivation:

After netty#14975, `EXPLICIT_NO_UNSAFE_CAUSE` has a dependency to
`JAVA_VERSION`, which is initialized after it. When
`explicitNoUnsafeCause0()` tries to set `io.netty.noUnsafe` to true by
default on Java 24, it fails, because `javaVersion()` always returns 0
at that point.

Also, `isAndroid0()` is called twice, once in `javaVersion0()` and once
to initialize `IS_ANDROID`. This is not a bug or a performance issue,
but it does have a visible side-effect when debug logging is enabled.

Modification:

The declaration of `EXPLICIT_NO_UNSAFE_CAUSE` is moved after
`JAVA_VERSION`, ensuring `javaVersion()` returns the correct version
when `explicitNoUnsafeCause0()` calls it.

`IS_ANDROID` is also moved before `JAVA_VERSION`, such that the
`isAndroid0()` method is called only once.

Result:

Fixes netty#14942 correctly
@WhiteTrashLord

Copy link
Copy Markdown

Is it possible that there are still warnings with Java 25 and Netty 4.2.7?
I get this:

WARNING: A restricted method in java.lang.System has been called
WARNING: java.lang.System::loadLibrary has been called by io.netty.util.internal.NativeLibraryUtil in an unnamed module (file:/)
WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
WARNING: Restricted methods will be blocked in a future release unless native access is enabled

JVM: OpenJDK Runtime Environment Temurin-25.0.1+8

@slandelle

Copy link
Copy Markdown
Contributor

@WhiteTrashLord This is completely unrelated to Unsafe. This warning is about the use of JNI that Oracle is replacing with the new Foreign Function & Memory API. For now, you should add this JVM option.

eolivelli added a commit to eolivelli/herddb that referenced this pull request Apr 26, 2026
…rect buffers (#298)

## Summary

Netty 4.1.120 and 4.1.121 disabled their internal `sun.misc.Unsafe`
usage by default
([netty/netty#14975](netty/netty#14975)) to
silence JEP 498 warnings on JDK 24+. With Unsafe disabled,
`PlatformDependent.useDirectBufferNoCleaner()` returns `false`: every
Netty direct buffer (pooled **and** unpooled) falls back to
`ByteBuffer.allocateDirect` → `java.nio.Bits.reserveMemory`, which is
bounded by `-XX:MaxDirectMemorySize`. The result is `OutOfMemoryError`
under heavy ingestion even though `-Dio.netty.maxDirectMemory` was set
generously.

Netty 4.1.122
([netty/netty#15296](netty/netty#15296)) reverts
the default. Until HerdDB upgrades (commit c0cf458 downgraded us
4.1.130 → 4.1.121 for issue #274), add
`--sun-misc-unsafe-memory-access=allow` to:

- `herddb-services/src/main/resources/bin/setenv.sh` — applies to every
JVM launched via `bin/service` (server, indexing-service, file-server,
bookkeeper) and to the CLI tools that inherit `JDK_JAVA_OPTIONS`
(`herddb-cli.sh`, `vector-bench.sh`, `indexing-admin.sh`).
- `pom.xml` — both Surefire `argLine`s (default and `ci` profile) so
unit tests on JDK 25 don't take the slow `Bits.reserveMemory` path
either.

## Verification

Reproduced locally on Eclipse Temurin **JDK 25.0.2** with the in-tree
Netty **4.1.121**.

**Before** (no flag): server saturates `-XX:MaxDirectMemorySize=1g`,
OOMs at 160k/1M rows even with `-Dio.netty.maxDirectMemory=4g`.
`netty_used_direct_memory_bytes` Prometheus gauge stuck at `-1` (counter
inactive).

**After** (with flag): server peak direct memory ~300 MiB, **0
OutOfMemoryErrors** through the full gist1m bench (1M vectors × 960-dim,
8 ingest threads, 5000 batch, post-ingest checkpoint, recall queries).
`netty_used_direct_memory_bytes` reports actual usage (counter active).

| Metric | Before | After |
|---|---|---|
| `netty_used_direct_memory_bytes` | `-1` | active (~134 MiB at end) |
| Server direct mem peak | hits 1024/1024 MiB cap | ~300 MiB / 4 GiB |
| OOMs in 1M-row ingest | 30–66 | **0** |
| Ingest throughput | bench fails | 20,332 rows/s |
| Recall@10 | bench fails | 0.91 |

## Also included

`herddb-services/examples/local-bench/install.sh` got a small batch of
bench-tuning changes that emerged from this investigation:

- `-Dherddb.file.requirefsync=false` for both server and
indexing-service (benchmark-only — never use this in production).
- Explicit `-XX:MaxDirectMemorySize` and `-Dio.netty.maxDirectMemory`
for both services (was leaving Netty at `=0`/JVM-bound; now explicit and
consistent).
- Indexing-service heap reduced from 40g → 30g so the `(server) + (IS)`
budget fits in a 61 GiB host with headroom for the OS.
- Documentation comments explaining the layout.

These are isolated to the `examples/local-bench` directory and don't
affect production defaults.

## Test plan

- [ ] CI runs all unit tests with the new Surefire `argLine` on JDK 25
- [ ] Smoke-test the `local-bench` example: `./teardown.sh &&
./install.sh && ./scripts/run-bench.sh --dataset gist1m -n 1000000
--ingest-threads 8 --batch-size 5000 --checkpoint --wait-for-indexes`

## Long-term follow-up

When HerdDB is upgraded back to **Netty 4.1.122+** (or **4.2.x**, which
is FFM-based and Unsafe-free), the
`--sun-misc-unsafe-memory-access=allow` flag in `setenv.sh` becomes
redundant and the comment block can be removed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Sonnet 4.6 <[email protected]>
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