Skip to content

Sampling mode for tracemalloc #150494

@danielsn

Description

@danielsn

Feature or enhancement

Proposal:

Tracemalloc tracks every allocation, which can be useful for debugging purposes, but imposes a high cost both in CPU (to collect the stacktrace for every allocation) and Memory (to store tracking metadata for every live object). In many cases, this overhead is unnecessary, and a statistical sample would be sufficient to explain both high memory consumption, as well as memory leaks.

I propose to add a poisson sampling mode to tracemalloc. In the common case, allocations would not be sampled, which means the CPU cost of tracemalloc would be just an increment and a comparison, while the additional memory cost would be 0. In cases where sampling does occur, the cost would be the same as before. The tracemalloc metadata would need to add an additional “weight” field to track the attributed weight of an allocation.

Has this already been discussed elsewhere?

This was discussed on the forum and got support from a core developer.

Links to previous discussion of this feature:

discuss.python.org/t/sampling-mode-for-tracemalloc/107333

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-featureA feature request or enhancement
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions