Skip to content

tsdb: fix memory leaks in buffer pools#17879

Merged
aknuds1 merged 4 commits intoprometheus:mainfrom
aknuds1:arve/buffer-reuse
Jan 19, 2026
Merged

tsdb: fix memory leaks in buffer pools#17879
aknuds1 merged 4 commits intoprometheus:mainfrom
aknuds1:arve/buffer-reuse

Conversation

@aknuds1
Copy link
Contributor

@aknuds1 aknuds1 commented Jan 17, 2026

Summary

  • Fix memory leaks in TSDB buffer pools by clearing reference fields before returning buffers to pools
  • Affected pools:
    • refSeriesPool: clear Labels field
    • histogramsPool: clear H pointer
    • floatHistogramsPool: clear FH pointer
    • metadataPool: clear Unit and Help strings

Without these fixes, the pooled buffers retain references to data that should be garbage collected, causing memory to accumulate over time.

This established pattern, of clearing references before pool re-use, is already followed in Head.putExemplarBuffer, Head.putSeriesBuffer and Head.putTypeMap. The former two were fixed in #13712.

Test plan

  • Added regression tests for all four affected pools
  • Tests verify that reference fields are properly cleared after buffer return
[BUGFIX] TSDB: Fix memory leaks in buffer pools by clearing reference fields (Labels, Histogram pointers, metadata strings) before returning buffers to pools

Clear reference fields when returning buffers to pools to avoid
retaining data after the buffer is reused.

Affected pools:
- refSeriesPool: clear Labels
- histogramsPool: clear H pointer
- floatHistogramsPool: clear FH pointer
- metadataPool: clear Unit and Help strings

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
@aknuds1 aknuds1 requested a review from bboreham January 19, 2026 10:07
Copy link
Member

@bboreham bboreham left a comment

Choose a reason for hiding this comment

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

Note similar changes were done in #13712; the other pools were added since.

I don't think the tests are worth checking in and adding over a hundred lines of bulk. They are testing internal details of the implementation of something that is quite obvious.
And only covering half the pools.

@aknuds1
Copy link
Contributor Author

aknuds1 commented Jan 19, 2026

OK, I can remove the tests @bboreham.

Note similar changes were done in #13712; the other pools were added since.

I noted #13712 in the PR description already :)

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
@aknuds1 aknuds1 requested a review from bboreham January 19, 2026 10:53
@aknuds1 aknuds1 enabled auto-merge (squash) January 19, 2026 11:00
@aknuds1 aknuds1 merged commit dd85d7c into prometheus:main Jan 19, 2026
32 checks passed
@aknuds1 aknuds1 deleted the arve/buffer-reuse branch January 21, 2026 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants