TL;DR
psutil 7.2 exposes the platform allocator so C-level memory allocations can be measured from Python. A companion PyPI tool, psleak, automates repeated runs and allocator trimming to flag native memory leaks in C extensions.
What happened
psutil 7.2.0 introduces new APIs that inspect the process's native allocator state, giving visibility into memory allocated by C extensions rather than Python's object allocator. The primary functions are heap_info(), which reports metrics such as bytes allocated via malloc() and mmap-backed allocations, and heap_trim(), which asks the underlying allocator to release unused heap memory before measurements. The project author says these calls bypass pymalloc and other Python-level accounting, making it possible to detect leaks that do not affect Python reference counts, tracemalloc, gc statistics, or even conventional RSS/VMS/USS readings on some platforms. To make testing practical, the author published psleak, a test harness that trims the allocator, runs a target function repeatedly, and fails tests if heap metrics grow consistently. psleak has been integrated into psutil's own test suite to catch regressions in C code.
Why it matters
- C extension leaks can be invisible to Python-level tools; these APIs reveal native allocator usage directly.
- Developers can now detect leaks caused by misplaced malloc/free or missing Py_DECREF/Py_CLEAR calls.
- A repeatable test workflow (trim, snapshot, run, snapshot) reduces false positives from allocator caching.
- Integration of psleak into psutil tests provides regression protection for native code in that project.
Key facts
- psutil 7.2.0 adds heap_info() and heap_trim() for native allocator inspection.
- heap_info reports heap_used (small malloc allocations) and mmap_used (large malloc/mmap allocations); heap_count is reported on Windows.
- heap_trim requests the allocator to free unused internal memory, primarily to reduce measurement noise.
- Native leaks often do not show up in sys.getrefcount, tracemalloc, Python gc stats, or sometimes in RSS/VMS/USS due to allocator caching.
- psleak is a PyPI package that runs target functions repeatedly, trims the allocator before runs, and flags consistent growth as a leak.
- psleak is included in the psutil test suite to detect memory regressions in the library's C code.
- Platform implementations: Linux uses glibc mallinfo2, Windows aggregates HeapAlloc/VirtualAlloc, macOS reads malloc zone stats, and BSD uses jemalloc interfaces.
What to watch next
- Wider adoption of psleak in other Python projects and libraries (not confirmed in the source).
- Potential enhancements to allocator compliance and effectiveness of heap_trim on different platforms (not confirmed in the source).
- Additional tooling or CI integrations built around psutil's heap APIs to automate native-leak detection (not confirmed in the source).
Quick glossary
- pymalloc: The CPython object allocator layer that manages small Python object allocations on top of the platform heap.
- RSS: Resident Set Size — the portion of a process's memory held in RAM.
- malloc / free: Standard C runtime calls to allocate and deallocate memory on the process heap.
- mmap: A system call that maps files or anonymous memory into a process address space, often used for large allocations.
- Py_DECREF / Py_CLEAR: Macros/functions used in CPython C extensions to decrement reference counts and release Python objects.
Reader FAQ
Will heap_info show Python object memory allocated by pymalloc?
No. The APIs inspect the platform allocator and do not reflect memory managed by pymalloc or Python object pools.
Can heap_trim reliably shrink RSS in production programs?
No. Modern allocators often do not release memory on request, so heap_trim is mainly useful to reduce allocator noise for measurements.
Is psleak part of psutil's tests?
Yes. The author says psleak is used in psutil's test suite to check for C-level memory leaks.
Which platforms are supported by the new allocator inspection?
The implementation covers Linux (glibc mallinfo2), Windows (heap and virtual alloc enumeration), macOS (malloc zone stats), and BSD (jemalloc interfaces).
Detect memory leaks of C extensions with psutil and psleak Created: 23 dic 2025, Tags: psutil, psleak, python, c Memory leaks in Python are often straightforward to diagnose. Just look…
Sources
- Detect memory leaks of C extensions with psutil and psleak
- psutil documentation — psutil 7.2.0 documentation
Related posts
- How Bookmakers Fight Smart Gamblers: The Battle to Stop Clever People Betting
- React Networks’ Fibre Demo: Diagram Components for Rackout.net
- China’s Overseas Clean-Energy Buildout Brings Environmental and Human-Rights Costs