Skip to content

[BUG] Memory leaks when containers are opened and decoded in a loop #2219

Closed

Description

@oakaigh

It appears that the container, when opened in decoding mode, does not release memory immediately when garbage collected.
Very frustrating because in applications requiring high parallelism (such as deep learning), the memory usage builds up extremely fast unless Python's GC is set to a lower threshold.

And such issue has already been documented, since at least 9.0:
https://pyav.org/docs/9.0.2/overview/caveats.html#garbage-collection
https://pyav.basswood-io.com/docs/17.0/overview/caveats.html#garbage-collection

Resolving this should be on the priority list IMO.

Sample memray output for reference:

Test was allowed to leak 1.0MiB per location but at least one location leaked more
----------------------------- memray-leaked-memory -----------------------------
List of leaked allocations:
    - 8.2MiB allocated here:
        av_malloc:<unknown>:0
        av_mallocz:<unknown>:0
        avcodec_alloc_context3:<unknown>:0
        __pyx_pf_2av_9container_5input_14InputContainer___cinit__:src/av/container/input.c:4289
        __pyx_pw_2av_9container_5input_14InputContainer_1__cinit__:src/av/container/input.c:3827
        ...
    - 8.2MiB allocated here:
        av_malloc:<unknown>:0
        av_mallocz:<unknown>:0
        avcodec_alloc_context3:<unknown>:0
        <unknown>:<unknown>:0
        avformat_find_stream_info:<unknown>:0
        ...

Activity

  1. lgeiger commented on Apr 1, 2026

    @lgeiger
    Contributor

    @oakaigh Do you have a minimal reproducible example that shows the memory leak you mentioned above?

  2. oakaigh commented on Apr 1, 2026

    @oakaigh
    Author

    @lgeiger The script is similar to this, except the container is opened/closed in a loop and the video stream is encoded using h265. Running it through memray should give you the results posted earlier.

    As for the reproducible example I have not gotten time to work on it yet. However I believe the above should be sufficient to make the problem appear.

  3. lgeiger commented on Apr 2, 2026

    @lgeiger
    Contributor

    Can you reproduce this when pyav is installed from source with latest main? Might be related to #2206 as well which has been fixed on main.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions