Skip to content

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
7.82.xfrom
backport-24553-to-7.82.x
Open

Backport #24553 to 7.82.x: return freed memory to the OS with malloc_trim after each run#24591
piochelepiotr wants to merge 1 commit into
7.82.xfrom
backport-24553-to-7.82.x

Conversation

@piochelepiotr

Copy link
Copy Markdown
Contributor

What does this PR do?

Backports #24553 to the 7.82.x branch. Calls glibc malloc_trim(0) after every kafka_consumer check run (in a finally), so per-arena free memory is returned to the OS each cycle. It is resolved once at import via ctypes and 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 a MALLOC_ARENA_MAX environment variable. Overhead is negligible for this check (runs are network-bound at several seconds).

The automated backport to 7.82.x failed on a cherry-pick conflict (7.82.x lacks unrelated base64/marshal imports and constants added to master by other commits). Resolved manually, keeping only this PR's changes.

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Add qa/required if this PR needs QA validation, or qa/skip-qa if it does not. Exactly one of the two is required.
  • If you need to backport this PR to another branch, you can add the backport/<branch-name> label to the PR and it will automatically open a backport PR once this one is merged

…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 piochelepiotr added qa/skip-qa Automatically skip this PR for the next QA integration/kafka_consumer team/agent-integrations qa/required QA is required for this PR and will generate a QA card and removed qa/skip-qa Automatically skip this PR for the next QA labels Jul 17, 2026
@piochelepiotr
piochelepiotr marked this pull request as ready for review July 17, 2026 14:14
@piochelepiotr
piochelepiotr requested a review from a team as a code owner July 17, 2026 14:14
@temporal-github-worker-1

Copy link
Copy Markdown

Backport evaluation: View Slack thread

@dd-octo-sts

dd-octo-sts Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Validation Report

All 21 validations passed.

Show details
Validation Description Status
agent-reqs Verify check versions match the Agent requirements file
ci Validate CI configuration and code coverage settings
codeowners Validate every integration has a CODEOWNERS entry
config Validate default configuration files against spec.yaml
dep Verify dependency pins are consistent and Agent-compatible
http Validate integrations use the HTTP wrapper correctly
imports Validate check imports do not use deprecated modules
integration-style Validate check code style conventions
jmx-metrics Validate JMX metrics definition files and config
labeler Validate PR labeler config matches integration directories
legacy-signature Validate no integration uses the legacy Agent check signature
license-headers Validate Python files have proper license headers
licenses Validate third-party license attribution list
metadata Validate metadata.csv metric definitions
models Validate configuration data models match spec.yaml
openmetrics Validate OpenMetrics integrations disable the metric limit
package Validate Python package metadata and naming
qa-label Validate the pull request declares whether it needs QA for the next Agent release
readmes Validate README files have required sections
saved-views Validate saved view JSON file structure and fields
version Validate version consistency between package and changelog

View full run

@datadog-prod-us1-6

Copy link
Copy Markdown

Tests  Code Coverage

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 77.27%
Overall Coverage: 91.69%

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: cfdddcd | Docs | Datadog PR Page | Give us feedback!

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

Labels

integration/kafka_consumer qa/required QA is required for this PR and will generate a QA card team/agent-integrations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant