Memory leaks in Python can be difficult to diagnose, especially when they occur in C extension modules. Traditional memory metrics such as RSS and VMS often fail to reveal these leaks because Python's memory allocator sits above the platform's native heap. To address this issue, psutil 7.2.0 introduces two new APIs for C heap introspection, which provide a window into the underlying platform allocator. These APIs, heap_info and heap_trim, allow developers to track memory usage and detect native leaks in C extensions. The heap_info function exposes metrics such as heap_used and mmap_used, while the heap_trim function provides a way to request that the underlying allocator free unused memory. By using these APIs, developers can detect memory leaks in C extensions that may not be visible through other means. A new tool called psleak has also been developed, which uses these APIs to detect memory leaks in C extensions by running a target function repeatedly and tracking changes in memory usage. The psleak tool is now part of the psutil test suite and can be used to test C extensions for memory leaks. Overall, the introduction of these new APIs and the psleak tool fills a long-standing observability gap and provides a simple method to test C extensions for leaks.
gmpy.dev
gmpy.dev
Create attached notes ...
