Re-enable sun.misc.Unsafe by default on Java 24+#15296
Merged
Conversation
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.
normanmaurer
approved these changes
May 30, 2025
This was referenced May 30, 2025
LuciferYang
pushed a commit
to apache/spark
that referenced
this pull request
Jun 11, 2025
…e to 2.0.72.Final ### What changes were proposed in this pull request? This PR aims to Upgrade Netty to 4.1.122.Final and Netty-tcnative to 2.0.72.Final. ### Why are the changes needed? To bring the Java 24 related update and the latest bug fixes. > We decided to enable sun.misc.Unsafe by default again when using JDK24+ (when possible) as disabling it by default on this java versions did come with quite a performance regression. - https://netty.io/news/2025/06/03/4-1-122-Final.html - netty/netty#15296 - netty/netty#15248 - netty/netty#15122 ### 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 #51145 from dongjoon-hyun/SPARK-52438. Authored-by: Dongjoon Hyun <dongjoon@apache.org> Signed-off-by: yangjie01 <yangjie01@baidu.com>
2 tasks
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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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
ByteBuffercleaners 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.