Backport #24553 to 7.82.x: return freed memory to the OS with malloc_trim after each run#24591
Open
piochelepiotr wants to merge 1 commit into
Open
Backport #24553 to 7.82.x: return freed memory to the OS with malloc_trim after each run#24591piochelepiotr wants to merge 1 commit into
piochelepiotr wants to merge 1 commit into
Conversation
…each run (#24553) * kafka_consumer: return freed memory to the OS with malloc_trim after each run librdkafka's many threads spread allocations across glibc arenas whose freed memory is retained per-arena and never returned to the OS, so agent RSS ratchets up over time. Call malloc_trim(0) after each check run (glibc/Linux only; a no-op on macOS and musl) so that per-arena free memory is handed back each cycle. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Add changelog entry for malloc_trim Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Address review: drop leading underscores on malloc helpers, remove comment Rename load_malloc_trim / malloc_trim without a leading underscore per the repo naming convention for module-level helpers, and drop the explanatory comment. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> (cherry picked from commit 433b81a)
piochelepiotr
marked this pull request as ready for review
July 17, 2026 14:14
|
Backport evaluation: View Slack thread |
Contributor
Validation ReportAll 21 validations passed. Show details
|
🎉 All green!🧪 All tests passed 🎯 Code Coverage (details) 🔗 Commit SHA: cfdddcd | Docs | Datadog PR Page | Give us feedback! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Backports #24553 to the
7.82.xbranch. Calls glibcmalloc_trim(0)after every kafka_consumer check run (in afinally), so per-arena free memory is returned to the OS each cycle. It is resolved once at import viactypesand is a no-op on platforms without it (macOS, musl).Motivation
librdkafka runs roughly one thread per broker, and on high-core hosts glibc spreads allocations across many per-thread arenas (default cap
8 x ncpu). glibc only ever returns memory from the top of an arena's heap and never coalesces free chunks across arenas, so the transient allocations each run leaves behind accumulate as retained-but-unused memory and agent RSS ratchets upward.malloc_trim(0)walks the arenas and hands their free memory back to the OS, keeping RSS bounded without requiring aMALLOC_ARENA_MAXenvironment variable. Overhead is negligible for this check (runs are network-bound at several seconds).The automated backport to
7.82.xfailed on a cherry-pick conflict (7.82.x lacks unrelatedbase64/marshalimports and constants added tomasterby other commits). Resolved manually, keeping only this PR's changes.Review checklist (to be filled by reviewers)
qa/requiredif this PR needs QA validation, orqa/skip-qaif it does not. Exactly one of the two is required.backport/<branch-name>label to the PR and it will automatically open a backport PR once this one is merged