Skip to content

Resampler object still occupies memory after deletion #1429

@MahmoudAshraf97

Description

@MahmoudAshraf97

Overview

I have a class that accepts audio inputs and resamples them as needed, the resampler parameters are different for each call so I can't create the resampler as a class member, anyways, after executing the function the resampler still leaves residuals in memory even if deleted

Expected behavior

memory usage should stay constant without having to run GC

Actual behavior

memory usage increases if GC is not run manually even if resampler object is deleted

Investigation

I ran the reproduction code to test three different cases:

Baseline:
image

delete=True, collect=False
image

collect=True
image

Reproduction

import psutil
import gc
import av
import matplotlib.pyplot as plt

audio_path = "/mnt/e/Projects/whisper-diarization/096.mp3"
process = psutil.Process()


def minimal_example(audio_path, delete=False):
    resampler = av.audio.resampler.AudioResampler(
        format="s16",
        layout="mono",
        rate=16000,
    )

    with av.open(audio_path, mode="r", metadata_errors="ignore") as container:
        frames = container.decode(audio=0)
        for frame in frames:
            frame = resampler.resample(frame)

    if delete:
        resampler = None
        del resampler


def monitor_memory(audio, n=20, collect=False, delete=False):
    gc.collect()
    init_memory_usage = process.memory_info().rss
    memory_usage = []
    for _ in range(n):
        minimal_example(audio, delete=delete)
        if collect:
            gc.collect()
        memory_usage.append(
            (process.memory_info().rss - init_memory_usage) / 1000000
        )  # Store memory usage in MB

    print("")
    gc.collect()
    # Plotting the memory usage
    plt.plot(memory_usage)
    plt.title("Memory Usage Over Time")
    plt.xlabel("Iteration")
    plt.ylabel("Memory Usage (MB)")
    plt.show()

Versions

  • OS: WSL Ubuntu 22.04
  • PyAV runtime:
PyAV v12.1.0
library configuration: --disable-static --enable-shared --libdir=/tmp/vendor/lib --prefix=/tmp/vendor --disable-alsa --disable-doc --disable-libtheora --disable-mediafoundation --disable-videotoolbox --enable-fontconfig --enable-gmp --enable-gnutls --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libspeex --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libxcb --enable-libxml2 --enable-lzma --enable-zlib --enable-version3 --enable-libx264 --disable-libopenh264 --enable-libx265 --enable-libxvid --enable-gpl
library license: GPL version 3 or later
libavcodec     60. 31.102
libavdevice    60.  3.100
libavfilter     9. 12.100
libavformat    60. 16.100
libavutil      58. 29.100
libswresample   4. 12.100
libswscale      7.  5.100

Research

I have done the following:

Additional context

SYSTRAN/faster-whisper#390
SYSTRAN/faster-whisper#856

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions