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 — Introduce size-classes for the adaptive allocator (#15399) by chrisvest · Pull Request #15414 · netty/netty

Conversation

@chrisvest

@chrisvest chrisvest commented Jun 28, 2025

Copy link
Copy Markdown
Member

Motivation:
The histograms in the adaptive allocator are best at looking for a single, good chunk size.
However, in practice, we have a lot of sizes that follow various patterns.
The histograms then tend to bias toward large sizes, which is wasteful in space.
It also turns out to be wasteful in time, in the sense that a small allocation can stick around for a long time, and prevent a large chunk from being reused when the rest of it has been deallocated. As such, the histogram-based magazines works best for fewer, larger allocations, and we need something else for small and plentiful allocations.

Modification:

  • Refactor the internals to detangle the magazines from the histogram implementation.
  • Introduce abstractions so the choice of recording and choosing buffer sizes, and the chunk allocation, is moved out of the magazines and into a chunk controller.
  • Move the allocation statistics into a histogram chunk controller, and introduce a separate size-classed chunk controller.
  • The size-classes are chosen from the observation that 1) most allocations aim to be power-of-2 sized, but 2) a lot of such allocations end up in framing protocols that add headers, checksums, etc. so the actual desired size is a power-of-2-plus-a-bit.
  • Introduce magazine groups. The group holds the magazine array and deals with contention by expanding the array. We will have a group per size class, along size a histogram-based group for large allocations.
  • Introduce a size-classed chunk which allocates out of a free-list instead of pointer-bump allocation. This greatly enhances the space reuse of such chunks and allows them to absorb a lot of the memory fragmentation that small allocations are prone to.
  • Add an MpscIntQueue class that we use for said free-list. This is based on the implementations found in JCTools.
  • Fix a concurrent-access bug in AdaptiveByteBuf.getBytes methods, where the internalNioBuffer had position and limit concurrently modified.

Result:
This should greatly reduce memory usage caused by chunks that hang around too long due to fragmentation.

Fixes #15311

Sorry, something went wrong.

Uh oh!

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

All reactions
@chrisvest
Introduce size-classes for the adaptive allocator (netty#15399)
9308bf1
Motivation:
The histograms in the adaptive allocator are best at looking for a
single, good chunk size.
However, in practice, we have a lot of sizes that follow various
patterns.
The histograms then tend to bias toward large sizes, which is wasteful
in space.
It also turns out to be wasteful in time, in the sense that a small
allocation can stick around for a long time, and prevent a large chunk
from being reused when the rest of it has been deallocated.
As such, the histogram-based magazines works best for fewer, larger
allocations, and we need something else for small and plentiful
allocations.

Modification:
- Refactor the internals to detangle the magazines from the histogram
implementation.
- Introduce abstractions so the choice of recording and choosing buffer
sizes, and the chunk allocation, is moved out of the magazines and into
a chunk controller.
- Move the allocation statistics into a histogram chunk controller, and
introduce a separate size-classed chunk controller.
- The size-classes are chosen from the observation that 1) most
allocations aim to be power-of-2 sized, but 2) a lot of such allocations
end up in framing protocols that add headers, checksums, etc. so the
actual desired size is a power-of-2-plus-a-bit.
- Introduce magazine groups. The group holds the magazine array and
deals with contention by expanding the array. We will have a group per
size class, along size a histogram-based group for large allocations.
- Introduce a size-classed chunk which allocates out of a free-list
instead of pointer-bump allocation. This greatly enhances the space
reuse of such chunks and allows them to absorb a lot of the memory
fragmentation that small allocations are prone to.
- Add an MpscIntQueue class that we use for said free-list. This is
based on the implementations found in JCTools.
- Fix a concurrent-access bug in AdaptiveByteBuf.getBytes methods, where
the internalNioBuffer had position and limit concurrently modified.

Result:
This should greatly reduce memory usage caused by chunks that hang
around too long due to fragmentation.

Fixes netty#15311
@chrisvest chrisvest added this to the 4.1.123.Final milestone Jun 28, 2025
@franz1981

franz1981 commented Jun 28, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Another thought on my FIFO suggestion; IDK now (I haven't tried really) how the size class Chunk remaining capacity is handled re the free list: if the free list has any segment available I expect the chunk to report that got some capacity left. Am I right? I couldn't find, reading the PR any evidence of it e.g. allocated bytes to decrease on free lo ist successful offer

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981
franz1981 requested review from franz1981 and normanmaurer and removed request for normanmaurer June 28, 2025 10:04
franz1981
franz1981 requested changes Jun 28, 2025
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
}
}

private static boolean shouldReleaseSuboptimalChunkSize(int givenSize, int preferredSize) {

@franz1981 franz1981 Jun 28, 2025 •
edited
Loading

Uh oh!

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

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.

here we could have something like

        @Override
        public int remainingCapacity() {
            int remainingCapacity = super.remainingCapacity();
            if (remainingCapacity > 0) {
                return remainingCapacity;
            }
            if (freeList.isEmpty()) {
                return 0;
            }
            int updatedRemainingCapacity = freeList.size() * segmentSize;
            assert updatedRemainingCapacity > 0;
            // update allocatedBytes based on what's available in the free list
            allocatedBytes = capacity() - updatedRemainingCapacity;
            return updatedRemainingCapacity;
        }

which can help to NOT remove the current chunk whilst there is still some available segment.

But since there's an already in place logic which rotate current Chunk once there's a single segment left, it can be changed into:

        @Override
        public int remainingCapacity() {
            int remainingCapacity = super.remainingCapacity();
            if (remainingCapacity > segmentSize) {
                return remainingCapacity;
            }
            int updatedRemainingCapacity = freeList.size() * segmentSize;
            if (updatedRemainingCapacity == remainingCapacity) {
                return remainingCapacity;
            }
            // update allocatedBytes based on what's available in the free list
            allocatedBytes = capacity() - updatedRemainingCapacity;
            return updatedRemainingCapacity;
        }

without changing the existing logic (which has served well for non size classes chunks).
This will update the capacity snapshot once we approach 1 or no chunks: if there's really a single chunk available, it will cause a rotation, but it's fine.

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/AdaptivePoolingAllocator.java
int startingCapacity = getStartingCapacity(size, maxCapacity);
private boolean allocate(int size, int maxCapacity, AdaptiveByteBuf buf, boolean reallocate) {
int startingCapacity = chunkController.computeBufferCapacity(size, maxCapacity, reallocate);
Chunk curr = current;

@franz1981 franz1981 Jun 28, 2025

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.

If we can now reuse more of the existing chunk (for size class chunks) it's not right that we expect here to rotate current (if (remainingCapacity == startingCapacity)) for the last allocation

Sorry, something went wrong.

Uh oh!

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

All reactions
@franz1981

franz1981 commented Jun 28, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

On #15414 (comment) I've better explained what could improve this case:

  1. (N - 1) segments are already allocated and released
  2. 1 new segment is allocated, but not released
  3. a new segment is required, but since the previous chunks last segment is not deallocated yet, a new chunk is required and set as current - wasting the available segments in the previous chunk

This is similar to the problem we had before, and size classes has indeed distributed the probability to happen, decoupling buffers with different types from each others, but it can still happen.

The second solution in #15414 (comment) it's taking care of using the right logic which won't modify the existing rotation logic.

@dreamlike-ocean can you try a patched version which add the mentioned logic?
I'm checking if it can cause some test failure

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981

franz1981 commented Jun 28, 2025

Copy link
Copy Markdown
Contributor

FYI #15416

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981 @chrisvest
Reuse the same SizeClassedChunk if there're available segments (netty…
0282775
…#15416)

Motivation:

SizeClassedChunk is not using the freeList size to update the remaining
capacity, prompting unnecessary chunk allocations

Modification:

Once approaching one or less segment left, update the remaining
capacity, to save allocating a new chunk

Result:

Reduced fragmentation and allocation pressure for buffers which release
later
@chrisvest

chrisvest commented Jun 29, 2025

Copy link
Copy Markdown
Member Author

I merged #15416 into 4.2 and cherry-picked it here.

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.

@franz1981

franz1981 commented Jun 29, 2025

Copy link
Copy Markdown
Contributor

Yep sorry, I have been lazy and not used the issues to track my concerns, which, as you rightly pointed, are not related this specific pr 🙏

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer
normanmaurer merged commit c49cdfa into netty:4.1 Jun 30, 2025
@chrisvest
chrisvest deleted the 4.1-fragmentation branch June 30, 2025 17:16
@dongjoon-hyun dongjoon-hyun mentioned this pull request Jul 28, 2025
[SPARK-52983][BUILD] Upgrade Netty to 4.1.123.Final apache/spark#51693
Closed
dongjoon-hyun added a commit to apache/spark that referenced this pull request Jul 29, 2025
@dongjoon-hyun
[SPARK-52983][BUILD] Upgrade Netty to 4.1.123.Final
8348f2a
### What changes were proposed in this pull request?

This PR aims to Upgrade Netty to 4.1.123.Final.

### Why are the changes needed?

To bring the latest bug fixes and also improvement.
- https://netty.io/news/2025/07/16/4-1-123-Final.html
  - netty/netty#15428
  - netty/netty#15425
  - netty/netty#15414
  - netty/netty#15422
  - netty/netty#15410
  - netty/netty#15393
  - netty/netty#15376
  - netty/netty#15368
  - netty/netty#15330
  - netty/netty#15346
  - netty/netty#15357
  - netty/netty#15355
  - netty/netty#15308

### 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 #51693 from dongjoon-hyun/SPARK-52983.

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
@dongjoon-hyun @prabhjyotsingh
[SPARK-52983][BUILD] Upgrade Netty to 4.1.123.Final
7d77b0c
This PR aims to Upgrade Netty to 4.1.123.Final.

To bring the latest bug fixes and also improvement.
- https://netty.io/news/2025/07/16/4-1-123-Final.html
  - netty/netty#15428
  - netty/netty#15425
  - netty/netty#15414
  - netty/netty#15422
  - netty/netty#15410
  - netty/netty#15393
  - netty/netty#15376
  - netty/netty#15368
  - netty/netty#15330
  - netty/netty#15346
  - netty/netty#15357
  - netty/netty#15355
  - netty/netty#15308

No.

Pass the CIs.

No.

Closes apache#51693 from dongjoon-hyun/SPARK-52983.

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 11, 2026
@dongjoon-hyun @prabhjyotsingh
[SPARK-52983][BUILD] Upgrade Netty to 4.1.123.Final
c185d91
This PR aims to Upgrade Netty to 4.1.123.Final.

To bring the latest bug fixes and also improvement.
- https://netty.io/news/2025/07/16/4-1-123-Final.html
  - netty/netty#15428
  - netty/netty#15425
  - netty/netty#15414
  - netty/netty#15422
  - netty/netty#15410
  - netty/netty#15393
  - netty/netty#15376
  - netty/netty#15368
  - netty/netty#15330
  - netty/netty#15346
  - netty/netty#15357
  - netty/netty#15355
  - netty/netty#15308

No.

Pass the CIs.

No.

Closes apache#51693 from dongjoon-hyun/SPARK-52983.

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 11, 2026
@dongjoon-hyun @prabhjyotsingh
[SPARK-52983][BUILD] Upgrade Netty to 4.1.123.Final
a0ed6fb
This PR aims to Upgrade Netty to 4.1.123.Final.

To bring the latest bug fixes and also improvement.
- https://netty.io/news/2025/07/16/4-1-123-Final.html
  - netty/netty#15428
  - netty/netty#15425
  - netty/netty#15414
  - netty/netty#15422
  - netty/netty#15410
  - netty/netty#15393
  - netty/netty#15376
  - netty/netty#15368
  - netty/netty#15330
  - netty/netty#15346
  - netty/netty#15357
  - netty/netty#15355
  - netty/netty#15308

No.

Pass the CIs.

No.

Closes apache#51693 from dongjoon-hyun/SPARK-52983.

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 11, 2026
@dongjoon-hyun @Ko-dany
[SPARK-52983][BUILD] Upgrade Netty to 4.1.123.Final
02bfc9e
This PR aims to Upgrade Netty to 4.1.123.Final.

To bring the latest bug fixes and also improvement.
- https://netty.io/news/2025/07/16/4-1-123-Final.html
  - netty/netty#15428
  - netty/netty#15425
  - netty/netty#15414
  - netty/netty#15422
  - netty/netty#15410
  - netty/netty#15393
  - netty/netty#15376
  - netty/netty#15368
  - netty/netty#15330
  - netty/netty#15346
  - netty/netty#15357
  - netty/netty#15355
  - netty/netty#15308

No.

Pass the CIs.

No.

Closes apache#51693 from dongjoon-hyun/SPARK-52983.

Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
This was referenced Sep 14, 2026
Http2: Reserve promised stream for PUSH_PROMISE after local RST_STREAM #17471
Closed
cache failure 1233e4a5 #17565
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@franz1981 franz1981 franz1981 requested 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.1.123.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

3 participants

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