Skip to content

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

Open
anuq wants to merge 1 commit intoelastic:mainfrom
fidelity-contributions:fix-issue-381
Open

Fix unbounded StringBuilder retention in ThreadLocal (memory leak)#391
anuq wants to merge 1 commit intoelastic:mainfrom
fidelity-contributions:fix-issue-381

Conversation

@anuq
Copy link

@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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-java community Issues and PRs created by the community triage Issues and PRs that need to be triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential Memory Leak: Unbounded StringBuilder retention via ThreadLocal in EcsJsonSerializer

1 participant