← Back to Pull requests

Add a shared malloc_trim helper to datadog_checks_base #25718

Closed
unknown closed this pull request without merging on Nov 12, 2026 07:57 UTC
View on GitHub
unknown unknown commented on Oct 21, 2026 05:02 UTC
Author

What does this PR do?

  • Adds malloc_trim() to datadog_checks.base.utils.native_memory, wrapping glibc's malloc_trim(0) and degrading to a no-op without glibc (macOS, musl, bionic).

  • No integration imports it yet; see the migration plan.
  • Motivation

  • glibc binds a malloc arena per thread. Checks do not run on the same thread every time, so memory freed by one run is not reusable by the next, and the process grows by roughly one run's peak each time.

  • ibm_was needs this. In AGENT-17325 a customer saw RSS climb 311 MB -> 3,965 MB over 9 runs parsing a 27.5 MB PerfServlet response into a libxml2 tree. Reproduced in a glibc container, 8 parses each on its own live thread:
  • | Configuration | RSS after 8 runs |
    |---|---|
    | Today | 416 -> 3,016 MB, linear |
    | With malloc_trim | 45 MB, flat |

  • Not new code: kafka_consumer has carried a local copy since #24553. Moved here so other integrations can call it, with one deliberate change — ctypes.CDLL(None) rather than ctypes.util.find_library('c'), which shells out to ldconfig, or to gcc where ldconfig is absent, on every import. CDLL(None) resolves from the process-wide namespace, so an LD_PRELOAD allocator interposing malloc_trim wins; intended, and documented in the docstring.

  • Related: #25712, from the same investigation. Independent; either can merge first.
  • Migration plan

    The minimum-base-package job installs each integration's declared floor from PyPI, so importing this before a released base contains it fails with ModuleNotFoundError (reproducible with ddev test <target> --compat). Sequence:

    1. This PR: helper in base, no callers.
    2. Release datadog-checks-base.
    3. Follow-up: bump the floor in ibm_was and kafka_consumer, have ibm_was call malloc_trim() in a finally around its check, and delete kafka_consumer's copy.

    Checklist before requesting review

  • I self-reviewed the full diff, and this description matches the final change

  • Tests cover the change, or this description explains why none are needed

  • <!--
    tests/base/utils/test_native_memory.py: 6 tests covering the no-op contract, the glibc
    binding and its pinned c_size_t/c_int signature, both degradation paths (libc absent,
    symbol absent), and both branches of the None guard. Verified by mutation: removing the
    argtypes/restype pinning, the None guard, or widening the except clause each turn the
    suite red. glibc 6 passed; musl 5 passed, 1 skipped.
    -->
  • Automated review comments, including Codex review, were addressed or answered

  • I, the human author, understand this change, can explain it to reviewers, and believe it meets the repository standards
  • 🤖 Generated with Claude Code

    Diff: 355 lines (+266 / -89) across 4 files Reviewer: iliakur Review duration: 31855m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.43) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 355 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.351
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #25718"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.5% (MEDIUM) · Higher = larger blast radius
    42.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.4% · Safe to merge
    18.4%