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 — allocation free AdaptiveByteBuf::setBytes(byte[]) by franz1981 · Pull Request #16058 · netty/netty

Conversation

@franz1981

@franz1981 franz1981 commented Dec 17, 2025

Copy link
Copy Markdown
Contributor

Motivation:
The adaptive buffer's setBytes(byte[]) method requires the underlying internal NIO buffer to be materialized, but temporary buffers (which won't get flushed to the network) are not granted to have it, causing an unwanted allocation

Modification:
Java 13 expose ad-hoc method which allow using the root parent NIO buffer, saving materializing the internal NIO buffer.

Result:
Cheaper setBytes

Sorry, something went wrong.

Uh oh!

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

All reactions
@franz1981
allocation free AdaptiveByteBuf::setBytes(byte[])
6854f54
Motivation:
The adaptive buffer's setBytes(byte[]) method requires the underlying internal NIO buffer to be materialized, but temporary buffers (which won't get flushed to the network) are not granted to have it, causing an unwanted allocation

Modification:
Java 13 expose ad-hoc method which allow using the root parent NIO buffer, saving materializing the internal NIO buffer.

Result:
Cheaper setBytes
@franz1981
franz1981 requested a review from chrisvest December 17, 2025 07:15
@franz1981

franz1981 commented Dec 17, 2025

Copy link
Copy Markdown
Contributor Author

This has popped out on some benchmark

image

And it's not the only place. More patches will arrive soon(ish).

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981
franz1981 requested a review from normanmaurer December 17, 2025 08:32
franz1981
franz1981 commented Dec 17, 2025
View reviewed changes
Comment thread common/src/main/java/io/netty/util/internal/PlatformDependent.java
if (PlatformDependent0.hasAbsolutePutArrayMethod()) {
return PlatformDependent0.absolutePut(dst, dstOffset, src, srcOffset, length);
} else {
ByteBuffer tmp = (ByteBuffer) dst.duplicate().clear().position(dstOffset).limit(dstOffset + length);

@franz1981 franz1981 Dec 17, 2025

Copy link
Copy Markdown
Contributor Author

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.

This part is not tested

Sorry, something went wrong.

Uh oh!

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

All reactions
normanmaurer
normanmaurer approved these changes Dec 17, 2025
View reviewed changes
@normanmaurer normanmaurer added this to the 4.2.10.Final milestone Dec 17, 2025
@normanmaurer
normanmaurer requested a review from yawkat December 17, 2025 14:42
yawkat
yawkat reviewed Dec 17, 2025
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
checkIndex(index, length);
ByteBuffer tmp = (ByteBuffer) internalNioBuffer().clear().position(index);
tmp.put(src, srcIndex, length);
if (tmpNioBuf == null && PlatformDependent.javaVersion() >= 13) {

@yawkat yawkat Dec 17, 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.

hasAbsolutePutArrayMethod instead of javaVersion, maybe?

Sorry, something went wrong.

Uh oh!

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

All reactions
yawkat
yawkat approved these changes Dec 17, 2025
View reviewed changes
chrisvest
chrisvest approved these changes Dec 18, 2025
View reviewed changes
@chrisvest
chrisvest merged commit 3cae6f6 into netty:4.2 Dec 18, 2025
chrisvest pushed a commit to chrisvest/netty that referenced this pull request Dec 18, 2025
@franz1981 @chrisvest
allocation free AdaptiveByteBuf::setBytes(byte[]) (netty#16058)
f9c9f55
Motivation:
The adaptive buffer's setBytes(byte[]) method requires the underlying
internal NIO buffer to be materialized, but temporary buffers (which
won't get flushed to the network) are not granted to have it, causing an
unwanted allocation

Modification:
Java 13 expose ad-hoc method which allow using the root parent NIO
buffer, saving materializing the internal NIO buffer.

Result:
Cheaper setBytes

(cherry picked from commit 3cae6f6)
@chrisvest chrisvest added the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Dec 18, 2025
@BaurzhanSakhariev

BaurzhanSakhariev commented Dec 18, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Hi @franz1981, if I may ask regarding

The adaptive buffer's setBytes(byte[]) method requires the underlying internal NIO buffer to be materialized, but temporary buffers (which won't get flushed to the network) are not granted to have it, causing an unwanted allocation

Could it be that those unwanted allocations happen on heap or it's always materialized into direct buffers?

I was recently getting on-heap OOM when using direct buffers and saw setBytes in the exception trace and this PR caught my attention.

Copying only relevant for this PR part from the question

java.lang.OutOfMemoryError: Java heap space
	at java.base/java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:75)
	at java.base/java.nio.ByteBuffer.allocate(ByteBuffer.java:398)
	at io.netty.buffer.CompositeByteBuf.nioBuffer(CompositeByteBuf.java:1709)
	at io.netty.buffer.AdaptivePoolingAllocator$AdaptiveByteBuf.setBytes(AdaptivePoolingAllocator.java:1765)
	at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1111)
	at io.netty.channel.nio.AbstractNioChannel.newDirectBuffer(AbstractNioChannel.java:519)
	at io.netty.channel.nio.AbstractNioByteChannel.filterOutboundMessage(AbstractNioByteChannel.java:280)
	at io.netty.channel.AbstractChannel$AbstractUnsafe.write(AbstractChannel.java:739)
	at io.netty.channel.DefaultChannelPipeline$HeadContext.write(DefaultChannelPipeline.java:1386)
	at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:823)
	at io.netty.channel.AbstractChannelHandlerContext$WriteTask.run(AbstractChannelHandlerContext.java:1136)
	at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:148)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:141)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java)
All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981

franz1981 commented Dec 18, 2025

Copy link
Copy Markdown
Contributor Author

@BaurzhanSakhariev it is possible but I would still be surprised unless there is some leak on the source NIO heap buffer (that's why the copy happen looking at the stack trace) which keep it referenced and prevent it to be GCd.
Enable heap dump on OOM and open an issue so we can take a look 🙏

This PR is not fixing the src side but the designation side (which receive the copy) so it is possible you were already full w the heap and the additional allocation on dst side just give the final blow

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

@BaurzhanSakhariev

BaurzhanSakhariev commented Dec 18, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

you were already full w the heap

Right, I run test with low Xmx.
Direct memory limit is equal (or close) to max heap size if not specified and I wanted to cap direct memory with some small value.

However, that was not a good idea to cap it like that as I run tests on same JVM with my application and created many structures in test itself and had very small max heap setting.

Once I start running tests with "normal" max heap and instead directly specified XX:MaxDirectMemorySize to cap direct memory, I stopped getting on heap OOM with/without the seed.

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

@BaurzhanSakhariev BaurzhanSakhariev mentioned this pull request Dec 18, 2025
ByteBufAllocatorMetric.usedDirectMemory reports low values for both pooled and adaptive allocators #16068
Closed
normanmaurer pushed a commit that referenced this pull request Dec 18, 2025
@chrisvest @franz1981
allocation free AdaptiveByteBuf::setBytes(byte[]) (#16058) (#16063)
bfc6eab
Motivation:
The adaptive buffer's setBytes(byte[]) method requires the underlying
internal NIO buffer to be materialized, but temporary buffers (which
won't get flushed to the network) are not granted to have it, causing an
unwanted allocation

Modification:
Java 13 expose ad-hoc method which allow using the root parent NIO
buffer, saving materializing the internal NIO buffer.

Result:
Cheaper setBytes

(cherry picked from commit 3cae6f6)

Co-authored-by: Francesco Nigro <[email protected]>
@BaurzhanSakhariev BaurzhanSakhariev mentioned this pull request Dec 22, 2025
Account for direct buffers when constructing an http result set crate/crate#18834
Merged
@nsnmurthyk nsnmurthyk mentioned this pull request Apr 16, 2026
Netty Issue: GZIP trailer corruption with large payloads after upgrading from 4.2.9 to 4.2.11 #16656
Closed
@chrisvest chrisvest removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Jun 29, 2026
@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
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
@normanmaurer normanmaurer normanmaurer approved these changes
@yawkat yawkat yawkat 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.10.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

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