Skip to content

[Core] Size FluxUtil.toFluxByteBuffer buffers to the data read - #51105

Merged
Isabelle (ibrandes) merged 4 commits into
Azure:mainfrom
ibrandes:core/copyIntoCorrectlySizedArray
Nov 10, 2026
Merged

Isabelle (ibrandes) merged 4 commits into
Azure:mainfrom
ibrandes:core/copyIntoCorrectlySizedArray

Conversation

@ibrandes

@ibrandes Isabelle (ibrandes) commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Description

#50837 (shipped in azure-core 1.61.0) changed FluxUtil.toFluxByteBuffer(InputStream, int) to emit a buffer as soon as a read returns and available() reports nothing more, instead of waiting to fill chunkSize. Each emitted buffer still wrapped a newly allocated chunkSize array, so any consumer that holds onto emitted buffers keeps a full chunkSize array alive for each partial read.

Storage is one such consumer. When an InputStream is uploaded without a length (e.g. BlobClient.uploadWithResponse(new BlobParallelUploadOptions(stream), ...)), storage converts it with a 64 MiB chunkSize and buffers the result up to maxSingleUploadSize to decide between a single-shot and a chunked upload. For a stream returning 1 MiB reads, every 1 MiB of data kept a 64 MiB array alive, a 64× amplification. This has caused the OutOfMemoryError failures in the storage live test pipeline since storage moved to core 1.61.0 (#51010). A heap dump from one of those runs showed a single storage upload buffer object (PayloadSizeGate) holding 7.78 GB (99% of the 8 GB heap) for about 116 MiB of actual data, during LargeBlobTests.uploadLargeInputSyncNoLengthGiven.

Changes

  • FluxUtil.toFluxByteBuffer(InputStream, int): each subscription allocates one chunkSize array (inside Flux.defer) and reuses it for every read. Each emission is an exact-size copy of the bytes read (Arrays.copyOf(buffer, offset)).
    • Emitting partial reads early, which [azure-core] Fix partial InputStream response buffering #50837 added, is unchanged.
    • Total allocation is about the data size plus one chunkSize array per subscription. Before this change, every read allocated a new chunkSize array.
    • Trade-off: full chunks are copied once more. In practice this costs less than allocating and zeroing a chunkSize array on every read.
    • The FileInputStream memory-mapping path is unchanged.
  • Added FluxUtilTest coverage:
    • Small reads produce buffers whose backing arrays are exactly the size of their data.
    • Mixing partial and full reads into the reused array doesn't overwrite earlier buffers.
    • Full chunks don't share a backing array.
  • Added a CHANGELOG entry.

Validation

  • mvn -f sdk/core/azure-core/pom.xml test checkstyle:check "-Dtest=FluxUtilTest" "-Djacoco.skip" "-Drevapi.skip" "-Dspotbugs.skip" "-Dcodesnippet.skip" "-Denforcer.skip": passed. 49 tests run, 0 failures, 1 skipped; 0 Checkstyle violations.

  • End-to-end storage repro. A standalone harness reproduces LargeBlobTests.uploadLargeInputSyncNoLengthGiven against Azurite: a 2,501 MiB SequenceInputStream of 1 MiB parts, uploaded with no length and a 2,500 MiB block size. It uses storage built from source with -Xmx8g, the same heap CI uses. Live heap is the peak after GC, taken from GC logs.

    azure-core Result Peak live heap Upload time
    1.60.0 (with netty 1.16.8) ✅ succeeded 2.7 GB 23 s
    1.61.0 ❌ OutOfMemoryError after about 4 s 8.1 GB —
    this PR ✅ succeeded (2 blocks: 2,500 MiB + 1 MiB) 3.1 GB 54–58 s

    About 2.5 GB of live heap is expected, since the upload must hold the whole 2,500 MiB block. The extra upload time compared with 1.60.0 comes from [azure-core] Fix partial InputStream response buffering #50837 emitting 2,501 buffers of 1 MiB instead of 40 of 64 MiB. It isn't caused by this change: see the next bullet. The 1.60.0 run had to use netty 1.16.8, so its timing isn't strictly comparable.

  • toFluxByteBuffer conversion only. Converting the same 2.5 GB stream with a 64 MiB chunkSize took about 260 ms on 1.60.0 (40 buffers), about 5,000 ms on 1.61.0 (2,501 buffers), and about 260 ms with this PR (2,501 buffers).

@github-actions github-actions Bot added the Azure.Core azure-core label Nov 4, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
33 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@ibrandes
Isabelle (ibrandes) marked this pull request as ready for review November 5, 2026 20:03
Copilot AI balanced review requested due to automatic review settings November 5, 2026 20:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the memory-retention fix in FluxUtil.toFluxByteBuffer.

No open issues 🎉

What changed in this PR

This PR fixes excessive memory retention in Azure Core’s FluxUtil.toFluxByteBuffer, addressing storage upload failures while preserving early emission of partial reads.

Changes:

  • Reuses one read array per subscription and copies emitted data into exact-size buffers.
  • Adds tests for partial-read sizing, mixed read sizes, and independent backing arrays.
  • Documents the memory fix in the changelog.
File Description
sdk/​core/​azure-core/​src/​test/​java/​com/​azure/​core/​util/​FluxUtilTest.java Adds buffer-sizing and data-independence regression tests.
sdk/​core/​azure-core/​src/​main/​java/​com/​azure/​core/​util/​FluxUtil.java Reuses the read array and emits exact-size copies.
sdk/​core/​azure-core/​CHANGELOG.md Records the memory-retention and upload failure fix.

Review effort: Balanced

@ibrandes
Isabelle (ibrandes) merged commit e7c65dd into Azure:main Nov 10, 2026
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Azure.Core azure-core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants