Skip to content

Issue #60 - Reduce the memory used to read the response body - #61

Merged
NassimBtk merged 2 commits into
mainfrom
feature/issue-60-reduce-the-memory-used-to-read-the-response-body
Oct 8, 2026
Merged

NassimBtk merged 2 commits into
mainfrom
feature/issue-60-reduce-the-memory-used-to-read-the-response-body

Conversation

@NassimBtk

Copy link
Copy Markdown
Collaborator

Closes #60

What changed

  • HttpClient decodes the body straight from the ByteArrayOutputStream buffer with bodyBytes.toString(charset.name()). Before, it copied the buffer with toByteArray() and then decoded the copy. toString(String) exists since Java 1.1 and goes through the same decoder, with the same replacement of malformed input. The charset name comes from Charset.forName(), so it always resolves to the same charset.
  • HttpResponse keeps the body as a String instead of a StringBuilder:
    • getBody() returns it without a copy;
    • appendBody() keeps its behavior: it concatenates, and appends "null" for null as StringBuilder.append() did. Each call after the first copies the body; the Javadoc says so, and the client calls it once per response;
    • toString() (header and body) uses concat(), which copies the body once instead of twice.
  • Compatibility. The public API does not change, and the library still targets Java 8.

Memory

For an ASCII body of N bytes on JDK 9+, the peak goes from about Cf + 2N to Cf + N, where Cf is the buffer capacity: N with an uncompressed Content-Length, N to 2N when chunked or compressed. In every case one N-byte copy disappears. The benchmark is in #60.

47.7 MiB Kubernetes list Transfer 1.0.02: min heap This PR: min heap
ASCII Content-Length 145 MiB 97 MiB
ASCII chunked 163 MiB 115 MiB
One char > U+00FF Content-Length 289 MiB 241 MiB
One char > U+00FF chunked 307 MiB 259 MiB

The allocation per call is halved (for example 238.7 → 95.5 MiB). With a UTF-16 body, the client now needs no more heap than Files.readString on the same content.

Tests

  • New HttpClientTest reads bodies from a local com.sun.net.httpserver server: with a Content-Length, chunked, charset from Content-Type (ISO-8859-1), gzip, empty, and an error status (the body comes from the error stream, and toString() is checked).
  • HttpResponseTest also checks appendBody(null), appending "", and that getBody() returns the String without copying it.

mvn verify with JDK 17 (as in CI): 10 tests pass. The Javadoc error about sun.net.www.protocol.http in the build log is not new: main shows it too. The integration tests (httpbin: gzip, deflate, UTF-8, downloadTo, status codes) run in the CI workflow.

🤖 Generated with Claude Code

Decode the body straight from the ByteArrayOutputStream buffer instead of
copying it with toByteArray() first, and keep it as a String in
HttpResponse: getBody() no longer copies it, appendBody() no longer copies
it into a StringBuilder, and toString() copies it once instead of twice.
For an ASCII body of N bytes, the peak goes from about Cf + 2N to Cf + N
(Cf = N with an uncompressed Content-Length).

Add a test of the body read path against a local server (Content-Length,
chunked, charset from Content-Type, gzip, empty body, error body).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-08T11:49:20.960939Z d075702 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@NassimBtk
NassimBtk merged commit 2350479 into main Oct 8, 2026
5 checks passed
@NassimBtk
NassimBtk deleted the feature/issue-60-reduce-the-memory-used-to-read-the-response-body branch October 8, 2026 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce the memory used to read the response body

1 participant