Skip to content

Fix unbounded StringBuilder retention in ThreadLocal (memory leak)#391

Merged
SylvainJuge merged 1 commit intoelastic:mainfrom
fidelity-contributions:fix-issue-381
Apr 21, 2026
Merged

Fix unbounded StringBuilder retention in ThreadLocal (memory leak)#391
SylvainJuge merged 1 commit intoelastic:mainfrom
fidelity-contributions:fix-issue-381

Conversation

@anuq
Copy link
Copy Markdown
Contributor

@anuq anuq commented Mar 25, 2026

getMessageStringBuilder() called setLength(0) which resets the logical length but never shrinks the internal char[] buffer. In thread-pool environments, a single large trace permanently bloats the buffer for that thread's lifetime, causing cumulative heap pressure.

Add a capacity threshold check (8 KB) that discards oversized buffers and replaces them with fresh instances, while still reusing normally-sized buffers for zero-allocation steady state.

Fixes #381

getMessageStringBuilder() called setLength(0) which resets the logical length but never shrinks the internal char[] buffer. In thread-pool environments, a single large trace permanently bloats the buffer for that thread's lifetime, causing cumulative heap pressure.

Add a capacity threshold check (8 KB) that discards oversized buffers and replaces them with fresh instances, while still reusing normally-sized buffers for zero-allocation steady state.

Fixes elastic#381
@anuq anuq requested a review from a team as a code owner March 25, 2026 21:04
@cla-checker-service
Copy link
Copy Markdown

cla-checker-service Bot commented Mar 25, 2026

💚 CLA has been signed

@github-actions github-actions Bot added agent-java community Issues and PRs created by the community triage Issues and PRs that need to be triaged labels Mar 25, 2026
@anuq
Copy link
Copy Markdown
Contributor Author

anuq commented Mar 26, 2026

❌ Author of the following commits did not sign a Contributor Agreement: df605d5

Please, read and sign the above mentioned agreement if you want to contribute to this project

done

@SylvainJuge SylvainJuge added bug Something isn't working and removed triage Issues and PRs that need to be triaged labels Mar 31, 2026
@SylvainJuge SylvainJuge reopened this Apr 21, 2026
@SylvainJuge SylvainJuge merged commit e080372 into elastic:main Apr 21, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-java bug Something isn't working community Issues and PRs created by the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential Memory Leak: Unbounded StringBuilder retention via ThreadLocal in EcsJsonSerializer

3 participants