首页 > 解决方案 > 在同一进程中使用 Gstreamer 和 multiprocessing.Manager().Namespace() 时发生内存泄漏

问题描述

我正在使用 Gstreamer 为一些视频制作故事板。我将每一帧放在 multiprocessing.Manager().Namespace() 中,以便在进程之间传输。在这种情况下,观察到内存泄漏。在 10 小时的应用程序工作期间,内存消耗增加了 400 MB。如果注释掉 的调用self.update_frame(buf.extract_dup(0, buf.get_size())),则不会发生内存泄漏。

内存泄漏测试的示例:

import unittest
import gi
import traceback
import os, psutil
from multiprocessing import Process, Manager, Event

gi.require_version('Gst', '1.0')
from gi.repository import Gst


class RtpNamespaceTest(unittest.TestCase):
    pipeline_str = '''
        videotestsrc pattern=ball ! \
        appsink name=handle-app-sink \
        emit-signals=True \
        max-buffers=1 \
        drop=True \
        '''

    name_space = Manager().Namespace()
    pipeline = None
    event_interrupt: Event = Event()

    def start(self):
        # initializing gstreamer, subscribing to rtp-stream
        Gst.init(None)
        print(self.pipeline_str)
        self.pipeline = Gst.parse_launch(self.pipeline_str)
        self.pipeline.set_state(Gst.State.PLAYING)
        self.appsink = self.pipeline.get_by_name('handle-app-sink')
        self.appsink.connect("new-sample", self.on_new_buffer)

        bus = self.pipeline.get_bus()
        # listen to messages until storyboarding is stopped
        while not self.event_interrupt.is_set():
            bus.timed_pop_filtered(10000, Gst.MessageType.ANY)

        # stops the pipeline, free up resources
        self.pipeline.set_state(Gst.State.NULL)

    def on_new_buffer(self, src):
        sample = src.emit("pull-sample")
        buf = sample.get_buffer()
        # writing a frame in binary format in Namespace()
        self.update_frame(buf.extract_dup(0, buf.get_size()))
        print(f"RAM = {psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024}")
        return Gst.FlowReturn.OK
        
    def update_frame(self, frame: bytes):
        self.name_space.frame = frame

    def test_repository(self):
        while True:
            try:
                self.start()
            except Exception as ex:
                traceback.print_exc()
                time.sleep(float(10))

标签: pythonmemory-leaksgstreamerpython-multiprocessing

解决方案


推荐阅读