kafka_consumer: return freed memory to the OS with malloc_trim after each run#24553
Conversation
…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>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
🎉 All green!🧪 All tests passed 🎯 Code Coverage (details) 🔗 Commit SHA: c235f70 | Docs | Datadog PR Page | Give us feedback! |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 43a61df3fd
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
…mment 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>
|
Backport evaluation: View Slack thread |
|
The backport to To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-7.82.x 7.82.x
# Navigate to the new working tree
cd .worktrees/backport-7.82.x
# Create a new branch
git switch --create backport-24553-to-7.82.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 433b81a58be72bd80dc86bdfc618a1af28412aac
# Push it to GitHub
git push --set-upstream origin backport-24553-to-7.82.x
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-7.82.xThen, create a pull request where the |
Validation ReportAll 21 validations passed. Show details
|
What does this PR do?
Call glibc
malloc_trim(0)after every 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).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