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 — Enforce io.netty.maxDirectMemory accounting on all Java versions by j-bahr · Pull Request #16489 · netty/netty

Conversation

@j-bahr

@j-bahr j-bahr commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Motivation:

Netty selects a Cleaner implementation based on the Java version and whether sun.misc.Unsafe is available. The selection matrix is:

  Java version   | Unsafe available | Cleaner selected
  ───────────────|──────────────────|─────────────────────
  6-8            | Yes              | DirectCleaner
  9-23           | Yes              | DirectCleaner
  24             | Yes (warnings)   | DirectCleaner
  24             | No, native access| CleanerJava24Linker
  25+            | No, native access| CleanerJava24Linker
  25+            | No               | CleanerJava25

Before this change, direct memory accounting (incrementMemoryCounter / decrementMemoryCounter) was coupled to USE_DIRECT_BUFFER_NO_CLEANER, which was only true when Unsafe was available. This had two consequences:

  1. DIRECT_MEMORY_COUNTER was only initialized inside the USE_DIRECT_BUFFER_NO_CLEANER=true branch, so on any Java version without Unsafe the counter was always null even if the user explicitly set io.netty.maxDirectMemory.

  2. The accounting calls themselves lived only in PlatformDependent's legacy NoCleaner methods (allocateDirectNoCleaner, reallocateDirectNoCleaner, freeDirectNoCleaner), which were only called by DirectCleaner. The other Cleaner implementations (CleanerJava9, CleanerJava6, CleanerJava24Linker, CleanerJava25) never called these methods and performed no accounting.

The combined effect was that the configured limit was silently ignored on every path that didn't go through DirectCleaner:

  Java version   | Unsafe | Cleaner             | Counter init | Accounting
  ───────────────|────────|─────────────────────|──────────────|───────────
  6-8            | Yes    | DirectCleaner       | Yes          | Yes ✓
  9-23           | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | No     | CleanerJava24Linker | No           | No  ✗
  25+            | No     | CleanerJava24Linker | No           | No  ✗
  25+            | No     | CleanerJava25       | No           | No  ✗

On Java 25+, where Unsafe is disabled by default, this means io.netty.maxDirectMemory has no effect at all.

Modifications:

  • Decouple DIRECT_MEMORY_COUNTER initialization from Unsafe availability. The counter is now created based solely on the value of io.netty.maxDirectMemory, independent of USE_DIRECT_BUFFER_NO_CLEANER.

  • Move accounting into each Cleaner's CleanableDirectBufferImpl so that every allocation/deallocation pair tracks memory regardless of which Cleaner is active:

    • DirectCleaner: increment in CleanableDirectBufferImpl(int capacity) constructor, decrement in clean().
    • CleanerJava9: increment in constructor, decrement in clean().
    • CleanerJava6: increment in constructor, decrement in clean().
    • CleanerJava24Linker: increment before malloc(), decrement in clean(), with rollback on allocation failure.
    • CleanerJava25: increment in allocate() before MethodHandle invoke, decrement in clean() via finally block.
  • Change incrementMemoryCounter/decrementMemoryCounter from private to package-private so Cleaner implementations (same package) can call them directly.

  • Add a default reallocate(CleanableDirectBuffer, int) method to the Cleaner interface with an allocate-copy-free fallback. DirectCleaner overrides this with in-place Unsafe.reallocateMemory, adjusting the counter by the delta.

  • Add PlatformDependent.reallocateDirect() as the unified public entry point for reallocation.

  • Remove the legacy NoCleaner API surface from PlatformDependent: allocateDirectNoCleaner, allocateDirectBufferNoCleaner, reallocateDirectNoCleaner, reallocateDirectBufferNoCleaner, and freeDirectNoCleaner.

  • Remove USE_DIRECT_BUFFER_NO_CLEANER and DIRECT_CLEANER fields. CLEANER is now the single entry point; useDirectBufferNoCleaner() returns whether CLEANER is an instance of DirectCleaner.

  • Update UnpooledUnsafeNoCleanerDirectByteBuf to use the new unified API: remove the allocateDirectBuffer() override (parent's impl now does the right thing via PlatformDependent.allocateDirect()), and delegate reallocateDirect() to PlatformDependent.reallocateDirect().

  • Update PlatformDependentTest.testAllocateWithCapacity0() to use the new CleanableDirectBuffer-based API.

Result:

After this change, the accounting matrix becomes:

  Java version   | Unsafe | Cleaner             | Counter init | Accounting
  ───────────────|────────|─────────────────────|──────────────|───────────
  6-8            | Yes    | DirectCleaner       | Yes          | Yes ✓
  9-23           | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | No     | CleanerJava24Linker | Yes          | Yes ✓
  25+            | No     | CleanerJava24Linker | Yes          | Yes ✓
  25+            | No     | CleanerJava25       | Yes          | Yes ✓

io.netty.maxDirectMemory is now enforced on all Java versions and all Cleaner implementations. The legacy raw-ByteBuffer NoCleaner API surface is eliminated, and each CleanableDirectBuffer is responsible for its own accounting.

Sorry, something went wrong.

Uh oh!

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

All reactions
@j-bahr

j-bahr commented Mar 16, 2026

Copy link
Copy Markdown
Contributor Author

@normanmaurer, I took a stab at fixing the issue outlined in the PR above. I am new to the allocator APIs so I would appreciate a through review and would be happy to accept feedback or suggestions on how to improve this PR.

I did create a little test harness to help validate the changes across java versions. The simple java program can be found here DirectMemoryAccountingTest.java, which can then be run with shell script here run.sh. Based on the raw output from the script (netty-common-4.2.10.Final.jar and netty-common-4.2.11.Final-SNAPSHOT.jar), you can see that the memory accounting fails in a number of cases. After my fix everything passes except in the cases where we don't have native access or unsafe, which results in the NOOP cleaner being used. In these cases the bytebuffers are tracked by the VM. The result matrix is shown below.

   #  | Config                                          | OLD  | NEW
  ----|-------------------------------------------------|------|-----
   1  | Java 8, Unsafe (default)                        | PASS | PASS
   2  | Java 11, Unsafe + opens + tryReflect            | PASS | PASS
   3  | Java 17, Unsafe + opens + tryReflect            | PASS | PASS
   4  | Java 21, Unsafe + opens + tryReflect            | PASS | PASS
   5  | Java 11, Unsafe, no opens                       | FAIL | PASS ✓ fixed
   6  | Java 17, Unsafe, no opens                       | FAIL | PASS ✓ fixed
   7  | Java 21, Unsafe, no opens                       | FAIL | PASS ✓ fixed
   8  | Java 11, noUnsafe=true (NOOP)                   | FAIL | FAIL (expected)
   9  | Java 17, noUnsafe=true (NOOP)                   | FAIL | FAIL (expected)
  10  | Java 21, noUnsafe=true (NOOP)                   | FAIL | FAIL (expected)
  11  | Java 24, Unsafe + opens + tryReflect            | PASS | PASS
  12  | Java 24, Unsafe, no opens                       | FAIL | PASS ✓ fixed
  13  | Java 24, noUnsafe + native access               | FAIL | PASS ✓ fixed
  14  | Java 24, noUnsafe, no native access (NOOP)      | FAIL | FAIL (expected)
  15  | Java 25, Unsafe force-enabled                   | PASS | PASS
  16  | Java 25, noUnsafe, no native access             | FAIL | PASS ✓ fixed
  17  | Java 25, noUnsafe + native access               | FAIL | PASS ✓ fixed
  18  | Java 25, default (no flags)                     | FAIL | PASS ✓ fixed

Thanks, I am looking forward to any feedback you might have.

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

Sorry, something went wrong.

Uh oh!

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

@franz1981

franz1981 commented Mar 16, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Tbh I like a lot this work @j-bahr !
Well done!
As we are moving to a mostly no unsafe world (still able to use glibc powered memory segments) this means to be able to still make sense of the max direct memory settings as we are used to be

j-bahr reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

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

@j-bahr
Enforce io.netty.maxDirectMemory accounting on all Java versions
ce115a9
Motivation:

Netty selects a Cleaner implementation based on the Java version and
whether sun.misc.Unsafe is available. The selection matrix is:

  Java version   | Unsafe available | Cleaner selected
  ───────────────|──────────────────|─────────────────────
  6-8            | Yes              | DirectCleaner
  9-23           | Yes              | DirectCleaner
  24             | Yes (warnings)   | DirectCleaner
  24             | No, native access| CleanerJava24Linker
  25+            | No, native access| CleanerJava24Linker
  25+            | No               | CleanerJava25

Before this change, direct memory accounting (incrementMemoryCounter /
decrementMemoryCounter) was coupled to USE_DIRECT_BUFFER_NO_CLEANER,
which was only true when Unsafe was available. This had two consequences:

1. DIRECT_MEMORY_COUNTER was only initialized inside the
   USE_DIRECT_BUFFER_NO_CLEANER=true branch, so on any Java version
   without Unsafe the counter was always null — even if the user
   explicitly set io.netty.maxDirectMemory.

2. The accounting calls themselves lived only in PlatformDependent's
   legacy NoCleaner methods (allocateDirectNoCleaner,
   reallocateDirectNoCleaner, freeDirectNoCleaner), which were only
   called by DirectCleaner. The other Cleaner implementations
   (CleanerJava9, CleanerJava6, CleanerJava24Linker, CleanerJava25)
   never called these methods and performed no accounting.

The combined effect was that the configured limit was silently ignored
on every path that didn't go through DirectCleaner:

  Java version   | Unsafe | Cleaner             | Counter init | Accounting
  ───────────────|────────|─────────────────────|──────────────|───────────
  6-8            | Yes    | DirectCleaner       | Yes          | Yes ✓
  9-23           | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | No     | CleanerJava24Linker | No           | No  ✗
  25+            | No     | CleanerJava24Linker | No           | No  ✗
  25+            | No     | CleanerJava25       | No           | No  ✗

On Java 25+, where Unsafe is disabled by default, this means
io.netty.maxDirectMemory has no effect at all.

Modifications:

- Decouple DIRECT_MEMORY_COUNTER initialization from Unsafe
  availability. The counter is now created based solely on the value
  of io.netty.maxDirectMemory, independent of USE_DIRECT_BUFFER_NO_CLEANER.

- Move accounting into each Cleaner's CleanableDirectBufferImpl so that
  every allocation/deallocation pair tracks memory regardless of which
  Cleaner is active:
  - DirectCleaner: increment in CleanableDirectBufferImpl(int capacity)
    constructor, decrement in clean().
  - CleanerJava9: increment in constructor, decrement in clean().
  - CleanerJava6: increment in constructor, decrement in clean().
  - CleanerJava24Linker: increment before malloc(), decrement in clean(),
    with rollback on allocation failure.
  - CleanerJava25: increment in allocate() before MethodHandle invoke,
    decrement in clean() via finally block.

- Change incrementMemoryCounter/decrementMemoryCounter from private to
  package-private so Cleaner implementations (same package) can call
  them directly.

- Add a default reallocate(CleanableDirectBuffer, int) method to the
  Cleaner interface with an allocate-copy-free fallback. DirectCleaner
  overrides this with in-place Unsafe.reallocateMemory, adjusting the
  counter by the delta.

- Add PlatformDependent.reallocateDirect() as the unified public entry
  point for reallocation.

- Remove the legacy NoCleaner API surface from PlatformDependent:
  allocateDirectNoCleaner, allocateDirectBufferNoCleaner,
  reallocateDirectNoCleaner, reallocateDirectBufferNoCleaner, and
  freeDirectNoCleaner.

- Remove USE_DIRECT_BUFFER_NO_CLEANER and DIRECT_CLEANER fields. CLEANER
  is now the single entry point; useDirectBufferNoCleaner() returns
  whether CLEANER is an instance of DirectCleaner.

- Update UnpooledUnsafeNoCleanerDirectByteBuf to use the new unified
  API: remove the allocateDirectBuffer() override (parent's impl now
  does the right thing via PlatformDependent.allocateDirect()), and
  delegate reallocateDirect() to PlatformDependent.reallocateDirect().

- Update PlatformDependentTest.testAllocateWithCapacity0() to use the
  new CleanableDirectBuffer-based API.

Result:

After this change, the accounting matrix becomes:

  Java version   | Unsafe | Cleaner             | Counter init | Accounting
  ───────────────|────────|─────────────────────|──────────────|───────────
  6-8            | Yes    | DirectCleaner       | Yes          | Yes ✓
  9-23           | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | No     | CleanerJava24Linker | Yes          | Yes ✓
  25+            | No     | CleanerJava24Linker | Yes          | Yes ✓
  25+            | No     | CleanerJava25       | Yes          | Yes ✓

io.netty.maxDirectMemory is now enforced on all Java versions and all
Cleaner implementations. The legacy raw-ByteBuffer NoCleaner API
surface is eliminated, and each CleanableDirectBuffer is responsible
for its own accounting.
@j-bahr
j-bahr force-pushed the memory-tracking branch from 43bf895 to ce115a9 Compare March 16, 2026 21:07
@normanmaurer normanmaurer modified the milestone: 4.2.11.Final Mar 17, 2026
@normanmaurer
normanmaurer requested review from chrisvest March 17, 2026 14:49
@normanmaurer

normanmaurer commented Mar 17, 2026

Copy link
Copy Markdown
Member

@chrisvest @yawkat PTAL...

@j-bahr I like it!

All reactions

Sorry, something went wrong.

Uh oh!

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

chrisvest
chrisvest approved these changes Mar 23, 2026
View reviewed changes

@chrisvest chrisvest 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.

Two small comments, otherwise looks good.

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/DirectCleaner.java Resolved

Uh oh!

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

Comment thread common/src/main/java/io/netty/util/internal/DirectCleaner.java Resolved

Uh oh!

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

@chrisvest chrisvest added the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Mar 23, 2026
j-bahr and others added 2 commits March 23, 2026 12:54
@j-bahr @chrisvest
Update common/src/main/java/io/netty/util/internal/DirectCleaner.java
a492228
Co-authored-by: Chris Vest <[email protected]>
@j-bahr @chrisvest
Update common/src/main/java/io/netty/util/internal/DirectCleaner.java
ad578e0
Co-authored-by: Chris Vest <[email protected]>
@j-bahr

j-bahr commented Mar 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @chrisvest for the review, I applied your suggested fixes.

chrisvest 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.

chrisvest
chrisvest approved these changes Mar 23, 2026
View reviewed changes
@chrisvest
chrisvest enabled auto-merge (squash) March 23, 2026 20:15
@chrisvest
chrisvest merged commit 7937553 into netty:4.2 Mar 23, 2026
19 checks passed

Uh oh!

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

@netty-project-bot

netty-project-bot commented Mar 23, 2026

Copy link
Copy Markdown
Contributor

Could not create auto-port PR.
Got conflicts when cherry-picking onto 5.0.

All reactions

Sorry, something went wrong.

Uh oh!

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

chrisvest pushed a commit to chrisvest/netty that referenced this pull request Mar 23, 2026
@j-bahr @chrisvest
Enforce io.netty.maxDirectMemory accounting on all Java versions (net…
4328508
…ty#16489)

**Motivation**:

Netty selects a Cleaner implementation based on the Java version and
whether `sun.misc.Unsafe` is available. The selection matrix is:
```
  Java version   | Unsafe available | Cleaner selected
  ───────────────|──────────────────|─────────────────────
  6-8            | Yes              | DirectCleaner
  9-23           | Yes              | DirectCleaner
  24             | Yes (warnings)   | DirectCleaner
  24             | No, native access| CleanerJava24Linker
  25+            | No, native access| CleanerJava24Linker
  25+            | No               | CleanerJava25
```
Before this change, direct memory accounting (`incrementMemoryCounter` /
`decrementMemoryCounter`) was coupled to `USE_DIRECT_BUFFER_NO_CLEANER`,
which was only true when Unsafe was available. This had two
consequences:

1. `DIRECT_MEMORY_COUNTER` was only initialized inside the
`USE_DIRECT_BUFFER_NO_CLEANER=true` branch, so on any Java version
without Unsafe the counter was always null even if the user explicitly
set `io.netty.maxDirectMemory`.

2. The accounting calls themselves lived only in PlatformDependent's
legacy NoCleaner methods (`allocateDirectNoCleaner`,
`reallocateDirectNoCleaner`, `freeDirectNoCleaner`), which were only
called by DirectCleaner. The other Cleaner implementations
(`CleanerJava9`, `CleanerJava6`, `CleanerJava24Linker`, `CleanerJava25`)
never called these methods and performed no accounting.

The combined effect was that the configured limit was silently ignored
on every path that didn't go through DirectCleaner:
```
  Java version   | Unsafe | Cleaner             | Counter init | Accounting
  ───────────────|────────|─────────────────────|──────────────|───────────
  6-8            | Yes    | DirectCleaner       | Yes          | Yes ✓
  9-23           | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | No     | CleanerJava24Linker | No           | No  ✗
  25+            | No     | CleanerJava24Linker | No           | No  ✗
  25+            | No     | CleanerJava25       | No           | No  ✗
```
On Java 25+, where Unsafe is disabled by default, this means
`io.netty.maxDirectMemory` has no effect at all.

**Modifications**:

- Decouple `DIRECT_MEMORY_COUNTER` initialization from Unsafe
availability. The counter is now created based solely on the value of
`io.netty.maxDirectMemory`, independent of
`USE_DIRECT_BUFFER_NO_CLEANER`.

- Move accounting into each Cleaner's `CleanableDirectBufferImpl` so
that every allocation/deallocation pair tracks memory regardless of
which Cleaner is active:
- `DirectCleaner`: increment in `CleanableDirectBufferImpl(int
capacity)` constructor, decrement in `clean()`.
  - `CleanerJava9`: increment in constructor, decrement in `clean()`.
  - `CleanerJava6`: increment in constructor, decrement in `clean()`.
- `CleanerJava24Linker`: increment before `malloc()`, decrement in
`clean()`, with rollback on allocation failure.
- `CleanerJava25`: increment in `allocate()` before MethodHandle invoke,
decrement in `clean()` via finally block.

- Change `incrementMemoryCounter`/`decrementMemoryCounter` from private
to package-private so Cleaner implementations (same package) can call
them directly.

- Add a default `reallocate(CleanableDirectBuffer, int)` method to the
Cleaner interface with an allocate-copy-free fallback. DirectCleaner
overrides this with in-place `Unsafe.reallocateMemory`, adjusting the
counter by the delta.

- Add `PlatformDependent.reallocateDirect()` as the unified public entry
point for reallocation.

- Remove the legacy NoCleaner API surface from PlatformDependent:
`allocateDirectNoCleaner`, `allocateDirectBufferNoCleaner`,
`reallocateDirectNoCleaner`, `reallocateDirectBufferNoCleaner`, and
`freeDirectNoCleaner`.

- Remove `USE_DIRECT_BUFFER_NO_CLEANER` and `DIRECT_CLEANER` fields.
`CLEANER` is now the single entry point; `useDirectBufferNoCleaner()`
returns whether `CLEANER` is an instance of DirectCleaner.

- Update `UnpooledUnsafeNoCleanerDirectByteBuf` to use the new unified
API: remove the `allocateDirectBuffer()` override (parent's impl now
does the right thing via `PlatformDependent.allocateDirect()`), and
delegate `reallocateDirect()` to `PlatformDependent.reallocateDirect()`.

- Update `PlatformDependentTest.testAllocateWithCapacity0()` to use the
new CleanableDirectBuffer-based API.

**Result**:

After this change, the accounting matrix becomes:
```
  Java version   | Unsafe | Cleaner             | Counter init | Accounting
  ───────────────|────────|─────────────────────|──────────────|───────────
  6-8            | Yes    | DirectCleaner       | Yes          | Yes ✓
  9-23           | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | No     | CleanerJava24Linker | Yes          | Yes ✓
  25+            | No     | CleanerJava24Linker | Yes          | Yes ✓
  25+            | No     | CleanerJava25       | Yes          | Yes ✓
```
`io.netty.maxDirectMemory` is now enforced on all Java versions and all
Cleaner implementations. The legacy raw-ByteBuffer NoCleaner API surface
is eliminated, and each `CleanableDirectBuffer` is responsible for its
own accounting.

---------

Co-authored-by: Chris Vest <[email protected]>

(cherry picked from commit 7937553)
@chrisvest

chrisvest commented Mar 23, 2026

Copy link
Copy Markdown
Member

The 5.0 port: #16531

All reactions

Sorry, something went wrong.

Uh oh!

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

@chrisvest chrisvest removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Mar 23, 2026
normanmaurer pushed a commit that referenced this pull request Mar 24, 2026
@chrisvest @j-bahr
Enforce io.netty.maxDirectMemory accounting on all Java versions (#16489
43125d9
) (#16531)

**Motivation**:

Netty selects a Cleaner implementation based on the Java version and
whether `sun.misc.Unsafe` is available. The selection matrix is:
```
  Java version   | Unsafe available | Cleaner selected
  ───────────────|──────────────────|─────────────────────
  6-8            | Yes              | DirectCleaner
  9-23           | Yes              | DirectCleaner
  24             | Yes (warnings)   | DirectCleaner
  24             | No, native access| CleanerJava24Linker
  25+            | No, native access| CleanerJava24Linker
  25+            | No               | CleanerJava25
```
Before this change, direct memory accounting (`incrementMemoryCounter` /
`decrementMemoryCounter`) was coupled to `USE_DIRECT_BUFFER_NO_CLEANER`,
which was only true when Unsafe was available. This had two
consequences:

1. `DIRECT_MEMORY_COUNTER` was only initialized inside the
`USE_DIRECT_BUFFER_NO_CLEANER=true` branch, so on any Java version
without Unsafe the counter was always null even if the user explicitly
set `io.netty.maxDirectMemory`.

2. The accounting calls themselves lived only in PlatformDependent's
legacy NoCleaner methods (`allocateDirectNoCleaner`,
`reallocateDirectNoCleaner`, `freeDirectNoCleaner`), which were only
called by DirectCleaner. The other Cleaner implementations
(`CleanerJava9`, `CleanerJava6`, `CleanerJava24Linker`, `CleanerJava25`)
never called these methods and performed no accounting.

The combined effect was that the configured limit was silently ignored
on every path that didn't go through DirectCleaner:
```
  Java version   | Unsafe | Cleaner             | Counter init | Accounting
  ───────────────|────────|─────────────────────|──────────────|───────────
  6-8            | Yes    | DirectCleaner       | Yes          | Yes ✓
  9-23           | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | No     | CleanerJava24Linker | No           | No  ✗
  25+            | No     | CleanerJava24Linker | No           | No  ✗
  25+            | No     | CleanerJava25       | No           | No  ✗
```
On Java 25+, where Unsafe is disabled by default, this means
`io.netty.maxDirectMemory` has no effect at all.

**Modifications**:

- Decouple `DIRECT_MEMORY_COUNTER` initialization from Unsafe
availability. The counter is now created based solely on the value of
`io.netty.maxDirectMemory`, independent of
`USE_DIRECT_BUFFER_NO_CLEANER`.

- Move accounting into each Cleaner's `CleanableDirectBufferImpl` so
that every allocation/deallocation pair tracks memory regardless of
which Cleaner is active:
- `DirectCleaner`: increment in `CleanableDirectBufferImpl(int
capacity)` constructor, decrement in `clean()`.
  - `CleanerJava9`: increment in constructor, decrement in `clean()`.
  - `CleanerJava6`: increment in constructor, decrement in `clean()`.
- `CleanerJava24Linker`: increment before `malloc()`, decrement in
`clean()`, with rollback on allocation failure.
- `CleanerJava25`: increment in `allocate()` before MethodHandle invoke,
decrement in `clean()` via finally block.

- Change `incrementMemoryCounter`/`decrementMemoryCounter` from private
to package-private so Cleaner implementations (same package) can call
them directly.

- Add a default `reallocate(CleanableDirectBuffer, int)` method to the
Cleaner interface with an allocate-copy-free fallback. DirectCleaner
overrides this with in-place `Unsafe.reallocateMemory`, adjusting the
counter by the delta.

- Add `PlatformDependent.reallocateDirect()` as the unified public entry
point for reallocation.

- Remove the legacy NoCleaner API surface from PlatformDependent:
`allocateDirectNoCleaner`, `allocateDirectBufferNoCleaner`,
`reallocateDirectNoCleaner`, `reallocateDirectBufferNoCleaner`, and
`freeDirectNoCleaner`.

- Remove `USE_DIRECT_BUFFER_NO_CLEANER` and `DIRECT_CLEANER` fields.
`CLEANER` is now the single entry point; `useDirectBufferNoCleaner()`
returns whether `CLEANER` is an instance of DirectCleaner.

- Update `UnpooledUnsafeNoCleanerDirectByteBuf` to use the new unified
API: remove the `allocateDirectBuffer()` override (parent's impl now
does the right thing via `PlatformDependent.allocateDirect()`), and
delegate `reallocateDirect()` to `PlatformDependent.reallocateDirect()`.

- Update `PlatformDependentTest.testAllocateWithCapacity0()` to use the
new CleanableDirectBuffer-based API.

**Result**:

After this change, the accounting matrix becomes:
```
  Java version   | Unsafe | Cleaner             | Counter init | Accounting
  ───────────────|────────|─────────────────────|──────────────|───────────
  6-8            | Yes    | DirectCleaner       | Yes          | Yes ✓
  9-23           | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | Yes    | DirectCleaner       | Yes          | Yes ✓
  24             | No     | CleanerJava24Linker | Yes          | Yes ✓
  25+            | No     | CleanerJava24Linker | Yes          | Yes ✓
  25+            | No     | CleanerJava25       | Yes          | Yes ✓
```
`io.netty.maxDirectMemory` is now enforced on all Java versions and all
Cleaner implementations. The legacy raw-ByteBuffer NoCleaner API surface
is eliminated, and each `CleanableDirectBuffer` is responsible for its
own accounting.

---------

Co-authored-by: Chris Vest <[email protected]>

(cherry picked from commit 7937553)

Co-authored-by: Jeff Bahr <[email protected]>
This was referenced Mar 25, 2026
[SPARK-56213] Upgrade Netty to 4.2.12.Final apache/spark-kubernetes-operator#589
Closed
[SPARK-56214][BUILD] Upgrade Netty to 4.2.12.Final apache/spark#55016
Closed
dongjoon-hyun added a commit to apache/spark-kubernetes-operator that referenced this pull request Mar 25, 2026
@dongjoon-hyun
[SPARK-56213] Upgrade Netty to 4.2.12.Final
7a03aaf
### What changes were proposed in this pull request?

This PR upgrades `Netty` to 4.2.12.Final.

### Why are the changes needed?

To bring in the latest bug fixes.
- https://netty.io/news/2026/03/24/4-2-12-Final.html
  - netty/netty#16550
- https://netty.io/news/2026/03/24/4-2-11-Final.html
  - netty/netty#16489
  - netty/netty#16536
  - netty/netty#16412

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

No.

### How was this patch tested?

CI should pass with the existing tests.

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

Generated-by: Claude Code (Claude Opus 4.6)

Closes #589 from dongjoon-hyun/SPARK-56213.

Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
dongjoon-hyun added a commit to apache/spark that referenced this pull request Mar 26, 2026
@dongjoon-hyun
[SPARK-56214][BUILD] Upgrade Netty to 4.2.12.Final
f76497a
### What changes were proposed in this pull request?

This PR upgrades `Netty` to 4.2.12.Final.

### Why are the changes needed?

To bring in the latest bug fixes.
- https://netty.io/news/2026/03/24/4-2-12-Final.html
  - netty/netty#16550
- https://netty.io/news/2026/03/24/4-2-11-Final.html
  - netty/netty#16489
  - netty/netty#16536
  - netty/netty#16412

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

No.

### How was this patch tested?

CI should pass with the existing tests.

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

Generated-by: Claude Code (Claude Opus 4.6)

Closes #55016 from dongjoon-hyun/SPARK-56214.

Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
Ko-dany pushed a commit to acceldata-io/spark3 that referenced this pull request Sep 9, 2026
@dongjoon-hyun @Ko-dany
[SPARK-56214][BUILD] Upgrade Netty to 4.2.12.Final
92abe94
This PR upgrades `Netty` to 4.2.12.Final.

To bring in the latest bug fixes.
- https://netty.io/news/2026/03/24/4-2-12-Final.html
  - netty/netty#16550
- https://netty.io/news/2026/03/24/4-2-11-Final.html
  - netty/netty#16489
  - netty/netty#16536
  - netty/netty#16412

No.

CI should pass with the existing tests.

Generated-by: Claude Code (Claude Opus 4.6)

Closes apache#55016 from dongjoon-hyun/SPARK-56214.

Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
(cherry picked from commit f76497a)
prabhjyotsingh pushed a commit to acceldata-io/spark3 that referenced this pull request Sep 10, 2026
@dongjoon-hyun @prabhjyotsingh
[SPARK-56214][BUILD] Upgrade Netty to 4.2.12.Final
9f02b88
This PR upgrades `Netty` to 4.2.12.Final.

To bring in the latest bug fixes.
- https://netty.io/news/2026/03/24/4-2-12-Final.html
  - netty/netty#16550
- https://netty.io/news/2026/03/24/4-2-11-Final.html
  - netty/netty#16489
  - netty/netty#16536
  - netty/netty#16412

No.

CI should pass with the existing tests.

Generated-by: Claude Code (Claude Opus 4.6)

Closes apache#55016 from dongjoon-hyun/SPARK-56214.

Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
(cherry picked from commit f76497a)
@normanmaurer normanmaurer mentioned this pull request Sep 22, 2026
suspend race fix #17588
Closed
@normanmaurer normanmaurer mentioned this pull request Oct 1, 2026
child5 #17695
Closed
This was referenced Oct 20, 2026
fix iouring zerocopy iov overwrite #17936
Closed
crisp nova cceda091 #18109
Closed
@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

@chrisvest chrisvest chrisvest 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.11.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

5 participants

@j-bahr @franz1981 @normanmaurer @netty-project-bot @chrisvest
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.